前言:我们在开发网站时,在管理后台,管理员通常要定期对数据库进行备份(当然也可以让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 ='您要保存的路径'
新闻热点
疑难解答
图片精选