using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
namespace wangshun.SystemConfig
{
/// <summary>
/// DataBase 的摘要说明。
/// </summary>
public class DataBase : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button Button2;
protected System.Web.UI.WebControls.Button Button1;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
}
/**//// <summary>
/// 杀死当前库的所有进程
/// </summary>
/// <returns></returns>
private bool exepro()
{
string connstr = System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];
string [] connstrayy = connstr.Split(";".ToCharArray(),4);
string ip=connstrayy[0].Split("=".ToCharArray(),2)[1];
string Database=connstrayy[1].Split("=".ToCharArray(),2)[1];
string Userid=connstrayy[2].Split("=".ToCharArray(),2)[1];
string Pwd=connstrayy[3].Split("=".ToCharArray(),2)[1];
SqlConnection conn1 = new SqlConnection("server="+ip+";uid="+Userid+";pwd="+Pwd+";database=master");
SqlCommand cmd = new SqlCommand("killspid",conn1);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@dbname",Database);
try
{
conn1.Open();
cmd.ExecuteNonQuery();
return true;
}
catch(Exception ex)
{
return false;
}
finally
{
conn1.Close();
}
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Button2.Click += new System.EventHandler(this.Button2_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
/// <summary>
/// 备份数据库
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Button1_Click(object sender, System.EventArgs e)
{
SQLDMO.Backup oBackup = new SQLDMO.BackupClass();
SQLDMO.SQLServer oSQLServer = new SQLDMO.SQLServerClass();
try
{
string connstr = System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];
string [] connstrayy = connstr.Split(";".ToCharArray(),4);
string ip=connstrayy[0].Split("=".ToCharArray(),2)[1];
string Database=connstrayy[1].Split("=".ToCharArray(),2)[1];
string Userid=connstrayy[2].Split("=".ToCharArray(),2)[1];
string Pwd=connstrayy[3].Split("=".ToCharArray(),2)[1];
oSQLServer.LoginSecure = false;
oSQLServer.Connect(ip, Userid, Pwd);
oBackup.Action = SQLDMO.SQLDMO_BACKUP_TYPE.SQLDMOBackup_Database;
oBackup.Database = Database;
oBackup.Files = Server.MapPath("./dbback") +"/"+Database+".bak";
oBackup.BackupSetName = Database;
oBackup.BackupSetDescription = "数据库备份";
oBackup.Initialize = true;
oBackup.SQLBackup(oSQLServer);
Response.Write("<script>alert('恭喜您备份数据库成功!');</script>");
}
catch
{
throw;
}
finally
{
oSQLServer.DisConnect();
}
}
/// <summary>
/// 还原数据库
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Button2_Click(object sender, System.EventArgs e)
{
if(exepro()!=true)//执行存储过程
{
Response.Write("<script>alert('对不起执行操作失败请重试!');</script>");
return;
}
else
{
SQLDMO.Restore oRestore = new SQLDMO.RestoreClass();
SQLDMO.SQLServer oSQLServer = new SQLDMO.SQLServerClass();
try
{
string connstr = System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];
string [] connstrayy = connstr.Split(";".ToCharArray(),4);
string ip=connstrayy[0].Split("=".ToCharArray(),2)[1];
string Database=connstrayy[1].Split("=".ToCharArray(),2)[1];
string Userid=connstrayy[2].Split("=".ToCharArray(),2)[1];
string Pwd=connstrayy[3].Split("=".ToCharArray(),2)[1];
oSQLServer.LoginSecure = false;
oSQLServer.Connect(ip,Userid,Pwd);
oRestore.Action = SQLDMO.SQLDMO_RESTORE_TYPE.SQLDMORestore_Database;
oRestore.Database = Database;
oRestore.Files = Server.MapPath("./dbback") +"/"+Database+".bak";
oRestore.FileNumber = 1;
oRestore.ReplaceDatabase = true;
oRestore.SQLRestore(oSQLServer);
Response.Write("<script>alert('恭喜您数据库还原成功!');</script>");
}
catch
{
throw;
}
finally
{
oSQLServer.DisConnect();
}
}
}
}
}