private void m3Button3_Click(object sender, EventArgs e)
{
string path = @"c:/Test.bak";
string backupstr = "backup database Test to disk='" + path + "';";
SqlConnection con = new SqlConnection("server=.;uid=sa;pwd=sa;");
SqlCommand cmd = new SqlCommand(backupstr, con);
try
{
con.Open();
cmd.ExecuteNonQuery();
MessageBox.Show("备份成功!");
}
catch
{
MessageBox.Show("备份失败!");
}
finally
{
con.Close();
}
}
private void m3Button1_Click(object sender, EventArgs e)
{
string path = @"c:/Test.bak";
string restore = "restore database Test from disk='" + path + "';";
SqlConnection con = new SqlConnection("server=.;uid=sa;pwd=sa;");
SqlCommand cmd = new SqlCommand(restore, con);
try
{
con.Open();
cmd.ExecuteNonQuery();
MessageBox.Show("恢复成功");
}
catch
{
MessageBox.Show("恢复失败");
}
finally
{
con.Close();
}
}
本文来自优快云博客,转载请标明出处:http://blog.youkuaiyun.com/hs123/archive/2007/05/07/1599342.aspx
本文介绍了一个简单的应用程序,用于实现SQL Server数据库的备份和恢复操作。通过C#代码创建了一个按钮事件,点击按钮即可将指定数据库备份到文件或从文件恢复到数据库。
1万+

被折叠的 条评论
为什么被折叠?



