protected void Button1_Click(object sender, EventArgs e)
{
string bakname="twohand";
SqlConnection myConnection = new SqlConnection();
string strconn = ConfigurationSettings.AppSettings["sqlserver"];
myConnection.ConnectionString = strconn;
string sql = "backup database towhand to disk = '" + Server.MapPath("").ToString() + "//" + bakname+ ".bak'";
try
{
SqlCommand cmd = new SqlCommand(sql, myConnection);
myConnection.Open();
cmd.ExecuteNonQuery();
myConnection.Close();
bacname = bakname + ".bak'";
string filename = Server.MapPath("").ToString() + "//" + bacname;
Response.Clear();
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment;FileName=" + HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8));
Response.WriteFile(filename);
Response.End();
}
catch(Exception ex)
{
Response.Write("<script language=javascript>alert('备份失败!');</script>");
}
}