C#数据库备份恢复这样简单

发布时间:2009年06月04日      浏览次数:372 次
这是我附的源代码,实现的备份和恢复很简单,大家可以自己在补充....(有BUG的话,欢迎指教)
这是WEB后台程序
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_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=;");
SqlCommand cmd = new SqlCommand(backupstr, con);
try
{
con.Open();
cmd.ExecuteNonQuery();
this.Label1.Text = "备份成功!";
}
catch
{
this.Label1.Text = "备份失败!";
}
finally
{
con.Close();
}
}
protected void Button2_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=;");
SqlCommand cmd=new SqlCommand(restore,con);
try
{
con.Open();
cmd.ExecuteNonQuery();
this.Label1.Text = "恢复成功";
}
catch
{
this.Label1.Text = "恢复失败";
}
finally
{
con.Close();
}
}
}
免责声明:本站相关技术文章信息部分来自网络,目的主要是传播更多信息,如果您认为本站的某些信息侵犯了您的版权,请与我们联系,我们会即时妥善的处理,谢谢合作!