using System; using System.Collections; using System.Configuration; using System.Data; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; using System.Data.SqlClient; public partial class Report_DateProtect : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void btnCopy_Click(object sender, EventArgs e) { try { string backupFolder = System.IO.Path.Combine(Request.PhysicalApplicationPath, "data"); if (!System.IO.Directory.Exists(backupFolder))//判断文件夹是否存在 { //创建文件夹 System.IO.Directory.CreateDirectory(backupFolder); } string backupFileName = System.IO.Path.Combine(backupFolder, DateTime.Today.ToString("yyyyMMdd.bak")); string sqltxt = string.Format("BACKUP DATABASE zzgcp TO DISK='{0}'", backupFileName); SqlConnection conn = new SqlConnection(); conn.ConnectionString = "server=.;uid=sa;pwd=sa;database=zzgcp"; conn.Open(); SqlCommand cmd = new SqlCommand(sqltxt, conn); cmd.ExecuteNonQuery(); conn.Close(); //MessageBox.Show("备份成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); Response.Write("<script>alert('备份成功!')</script>"); //this.Close(); } catch { throw; } } protected void btnHuanyuan_Click(object sender, EventArgs e) { if (FileUpload1.FileName=="") { Response.Write("<script>alert('请选择要还原的文件!')<script>"); } else { string url = FileUpload1.FileName; RestoreDatabase(Server.MapPath("../data") + "\\" + url); } } public void RestoreDatabase(string backfile) { ///杀死原来所有的数据库连接进程 SqlConnection conn = new SqlConnection(); conn.ConnectionString = "Data Source=.;Initial Catalog=master;User ID=sa;pwd =sa"; conn.Open(); string sql = "SELECT spid FROM sysprocesses ,sysdatabases WHERE sysprocesses.dbid=sysdatabases.dbid AND sysdatabases.Name='" + "zzgcp" + "'"; SqlCommand cmd1 = new SqlCommand(sql, conn); SqlDataReader dr; ArrayList list = new ArrayList(); try { dr = cmd1.ExecuteReader(); while (dr.Read()) { list.Add(dr.GetInt16(0)); } dr.Close(); } catch (SqlException eee) { throw; } finally { conn.Close(); } //MessageBox.Show(list.Count.ToString()); for (int i = 0; i < list.Count; i++) { conn.Open(); cmd1 = new SqlCommand(string.Format("KILL {0}", list[i].ToString()), conn); cmd1.ExecuteNonQuery(); conn.Close(); //MessageBox.Show("系统已经清除的数据库线程: " + list[i].ToString() + "\r\n正在还原数据库!"); Response.Write("<script>alert('系统已经清除的数据库线程')</script>" + list[i].ToString() + "\\r\\n正在还原数据库!"); } //这里一定要是master数据库,而不能是要还原的数据库,因为这样便变成了有其它进程 //占用了数据库。 string constr = @"Data Source=.;Initial Catalog=master;User ID=sa;pwd =sa"; string database = "zzgcp"; string path = backfile; //database是要还原数据库的名字,与备份的数据库结构必需一样, if (conn.State == ConnectionState.Open) { conn.Close(); } string BACKUP = String.Format("RESTORE DATABASE {0} FROM DISK = '{1}'", database, path); SqlConnection con = new SqlConnection(constr); SqlCommand cmd = new SqlCommand(BACKUP, con); con.Open(); try { cmd.ExecuteNonQuery(); //MessageBox.Show("还原成功,点击退出系统!"); Response.Write("<script>alert('还原成功,点击退出系统!')</script>"); } catch (SqlException ee) { throw; //throw(ee); //MessageBox.Show("还原失败"); //Response.Write("<script>alert('还原失败!')</script>"); Response.Write("<script>alert('还原失败,点击退出系统!')</script>"); } finally { con.Close(); } SqlConnection.ClearAllPools(); //如不加这句会有错误:在向服务器发送请求时发生传输级错误。 provider 共享内存提供程序 error 0 管道的另一端上无任何进程。 } }