WebDataBak.aspx.cs bakServer.cs为自定义类代码 核心代码: 1. private void BtnDataBackup_Click(object sender, System.EventArgs e) { if ( lstDb.Items.Count == 0 ) { pub.Alert("数据库列表不能为空!", this.Page); } else if( txtInFile.Text =="" ) { pub.Alert("备份文件名称不能为空!", this.Page); } else if( txtDbName.Text =="" || txtUserName.Text == "") { pub.Alert("数据库,用户不能为空!!",this.Page); } else { BtnDataRestore.Enabled = false; BtnQuery.Enabled = false; btnCear.Enabled = false; if ( baks.BackUPDB(lstDb.SelectedItem.ToString(),txtInFile.Text,txtDbName.Text,txtUserName.Text,txtPwd.Text) == true ) { pub.Alert("备份成功!",this.Page); } else { pub.Alert("要备份的路径错误不存在,请输入正确路径!!",this.Page); } BtnDataRestore.Enabled = true; BtnQuery.Enabled = true; btnCear.Enabled = true; } } private void BtnDataRestore_Click(object sender, System.EventArgs e) { //恢复数据库 if ( lstDb.Items.Count == 0 ) { pub.Alert("数据库列表不能为空!", this.Page); } else if ( txtOutFile.Text =="" ) { pub.Alert("还原文件名称不能为空!", this.Page); } else if( txtDbName.Text =="" || txtUserName.Text == "") { pub.Alert("数据库,用户不能为空!!",this.Page); } else { BtnDataBackup.Enabled = false; BtnQuery.Enabled = false; btnCear.Enabled = false; if ( baks.RestoreDB(lstDb.SelectedItem.ToString(),txtOutFile.Text,txtDbName.Text,txtUserName.Text,txtPwd.Text) == true ) { pub.Alert("恢复成功!",this.Page); } else { pub.Alert("要恢复的数据库文件不存在,请输入正确路径!!",this.Page); } BtnDataBackup.Enabled = true; BtnQuery.Enabled = true; btnCear.Enabled = true; } } [@more@] 2. namespace WebSearch { /// /*-------------------------------------------------------------------------- 系统名称: xxxxx 设计时间:2005-09-03 代码设计者: winzheng 模块名称: asp.net下sqlserver 2000 数据库备份与还原 模块标识: DbBAK -------------------------------------------------------------------------- */ public class bakServer { string ServerName; //数据服务器名称 string UserName; //用户名称 string Password; //用户密码 string message; //消息提示 public bakServer() { // // TODO: 在此处添加构造函数逻辑 // } /// /// 取得数据库服务器列表 /// ///
数据库服务器列表 public ArrayList GetServerList() { ArrayList alServers = new ArrayList() ; SQLDMO.ApplicationClass sqlApp = new SQLDMO.ApplicationClass(); try { SQLDMO.NameList serverList = sqlApp.ListAvailableSQLServers() ; if(serverList !=null) { for(int i = 1;i<= serverList.Count;i++) { alServers.Add(serverList.Item(i)) ; } } else { message="你的系统需要打上SQL SERVER 2000 SP3这个补丁"; } } catch(Exception e) { message = "取数据库服务器列表出错:" +e.Message; } finally { sqlApp.Quit() ; } return alServers ; } /// /// 错误消息处理 /// ///
消息信息 public string Msg() { return message; } /// /// 取得指定数据库列表 /// /// 服务器名称 /// 用户名称 /// 用户密码 ///
数据库列表 public ArrayList GetDbList(string strServerName,string strUserName,string strPwd) { ServerName = strServerName ; UserName = strUserName ; Password = strPwd ; ArrayList alDbs = new ArrayList() ; SQLDMO.ApplicationClass sqlApp = new SQLDMO.ApplicationClass() ; SQLDMO.SQLServer svr = new SQLDMO.SQLServerClass() ; try { svr.Connect(ServerName,UserName,Password) ; foreach(SQLDMO.Database db in svr.Databases) { if(db.Name!=null) alDbs.Add(db.Name) ; } } catch(Exception err) { // throw(new Exception("连接数据库出错:"+e.Message)) ; message = "连接数据库出错:" +err.Message; } finally { svr.DisConnect() ; sqlApp.Quit() ; } return alDbs ; } /// /// 数据库名称 /// 备份文件名 /// 服务器名称 /// 用户名称 /// 密码 ///
备份成功返回true ,否则返回false public bool BackUPDB(string strDbName,string strFileName, string strServerName,string strUserName,string strPwd) { ServerName = strServerName ; UserName = strUserName ; Password = strPwd ; SQLDMO.SQLServer svr = new SQLDMO.SQLServerClass() ; try { svr.Connect(ServerName,UserName,Password) ; SQLDMO.Backup bak = new SQLDMO.BackupClass(); bak.Action = 0 ; bak.Initialize = true ; SQLDMO.BackupSink_PercentCompleteEventHandler pceh = new SQLDMO.BackupSink_PercentCompleteEventHandler(Step); bak.PercentComplete += pceh; bak.Files = strFileName; bak.Database = strDbName; bak.SQLBackup(svr); return true ; } catch(Exception err) { // throw(new Exception("备份数据库失败"+err.Message)) ; message = "备份数据库失败:" +err.Message; return false ; } finally { svr.DisConnect() ; } } /// 备份文件名 /// 状态条控件名称 /// 服务器名称 /// 用户名称 /// 密码 ///
恢复成功返回true ,否则返回false public bool RestoreDB(string strDbName,string strFileName,string strServerName,string strUserName,string strPwd ) { SQLDMO.SQLServer svr = new SQLDMO.SQLServerClass() ; try { ServerName = strServerName ; UserName = strUserName ; Password = strPwd ; svr.Connect(ServerName,UserName,Password) ; SQLDMO.QueryResults qr = svr.EnumProcesses(-1) ; int iColPIDNum = -1 ; int iColDbName = -1 ; for(int i=1;i<=qr.Columns;i++) { string strName = qr.get_ColumnName(i) ; if (strName.ToUpper().Trim() == "SPID") { iColPIDNum = i ; } else if (strName.ToUpper().Trim() == "DBNAME") { iColDbName = i ; } if (iColPIDNum != -1 && iColDbName != -1) break ; } for(int i=1;i<=qr.Rows;i++) { int lPID = qr.GetColumnLong(i,iColPIDNum) ; string strDBName = qr.GetColumnString(i,iColDbName) ; if (strDBName.ToUpper() == strDbName.ToUpper()) svr.KillProcess(lPID) ; } SQLDMO.Restore res = new SQLDMO.RestoreClass() ; res.Action = 0 ; SQLDMO.RestoreSink_PercentCompleteEventHandler pceh = new SQLDMO.RestoreSink_PercentCompleteEventHandler(Step); res.PercentComplete += pceh; res.Files = strFileName ; res.Database = strDbName ; res.ReplaceDatabase = true ; res.SQLRestore(svr) ; return true ; } catch(Exception err) { message = "恢复数据库失败,请关闭所有和该数据库连接的程序!" +err.Message; return false; } finally { svr.DisConnect() ; } } } } |