下面是我做一個網站後台時在"備份"按扭下寫的一個事件:
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("http://www.cnblogs.com/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 = "http://www.cnblogs.com/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 ='您要儲存的路徑'