下面是我做一个网站后台时在"备份"按扭下写的一个事件:
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 ='您要保存的路径'