前言:我们在开发网站时,在管理后台,管理员通常要定期对数据库进行备份(当然也可以让sqlserver服务器定期自动备份,但我此处讲的是asp.net中的备份),备份的代码很简单:
\n
下面是我做一个网站后台时在”备份”按扭下写的一个事件,应该很容易看懂:)
\n
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();
} 当然最关键的还是这句:
来源:j_jake的blog