Sqlserver数据库备份与还原,并用winform进行简单的登录测试
当测试完成以后换到新的服务器,需要对数据进行备份,因而在Sqlserver中需要迁移数据
1、备份
首先打开SSMS(上一篇博客有提及怎么安装https://blog.csdn.net/xiexiangyan/article/details/81974776),连接好数据库
新建一个数据库做测试,这里新建了一个test数据库。
点击test-右键-任务-备份
找到test文件即可备份完成
2、恢复
还原,点击数据库,右键-还原数据库-设备
确定,添加即可。
3、使用winform对数据库进行测试
打开vs2015软件,点击新建windorm项目,在工具箱中添加textbox与button控件
相应的添加事件
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace test2
{
public partial class Form1 : Form
{
string str_con = "Data Source=你的IP地址/本地的话就是你的计算机名;Initial Catalog=test;Persist Security Info=True;User ID=sa; Password=密码"; //声明一个数据集
DataSet ds = new DataSet();
SqlDataAdapter da;
SqlConnection con;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
try
{
string str1 = textBox1.Text.Trim();
string str2 = textBox2.Text.Trim();
// Console.WriteLine(str1+"***********"+str2); //测试时使用
string str_Login = "select name,password from test where name='" + textBox1.Text.Trim() + "' and password='" + textBox2.Text.Trim() + "'";
con = new SqlConnection(str_con);
con.Open();
da = new SqlDataAdapter(str_Login, con);
da.Fill(ds);
// Console.WriteLine(ds.Tables[0].Rows.Count); //测试时使用
if (ds.Tables[0].Rows.Count > 0)
{
MessageBox.Show("登录成功"); //连接成功
}
else
{
MessageBox.Show("登录失败,请重新输入用户与密码", "提示信息", MessageBoxButtons.OKCancel, MessageBoxIcon.Stop);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
ds.Clear();
con.Close();
}
}
} }
显示如下所示则登录成功。数据库连接完成
其demo如下链接https://download.csdn.net/download/xiexiangyan/10629912