Go homepage(回首页)
Upload pictures (上传图片)
Write articles (发文字帖)

The author:(作者)归海一刀
published in(发表于) 2014/1/30 1:57:06
用ASP.NET代码实现备份SQL,Server数据库_[Asp.Net教程]

用ASP.NET代码实现备份SQL Server数据库_[Asp.Net教程]
当然也可以让sqlserver服务器定期自动备份,但我此处讲的是asp.net中的备份,备份的代码很简单:

下面是我做一个网站后台时在"备份"按扭下写的一个事件:


protected void Button1_Click(object sender, EventArgs e)

...{

string newname = "WebJake" + DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString()+".bak";

SqlConnection cn = DB.createCon();

string nepath=Server.MapPath("../../DataBack/")+newname;

string sql = "BACKUP DATABASE WebJake to DISK =’"+nepath+"’";

SqlCommand cmd = new SqlCommand(sql,cn);

try

...{

cn.Open();

cmd.ExecuteNonQuery();

HyperLink1.Text = "已成功备份到服务器路径:" + nepath+" 请点击下载到本地!";

HyperLink1.NavigateUrl = "../../DataBack/"+newname;

HyperLink1.Visible = true;

}

catch (Exception ex)

...{

string exm = ex.Message;

Label1.Text = "备份数据库出错,该文件可能不存在!";

Label1.Visible = true;

}

finally

...{

cmd.Dispose();

cn.Close();

cn.Dispose();

}

最关键的是这句:

BACKUP DATABASE WebJake to DISK =’您要保存的路径’




If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)





QQ:154298438
QQ:417480759