11. 2 3private void BtnDataBackup_Click(object sender, System.EventArgs e) 4 ...{ 5 if ( lstDb.Items.Count == 0 ) 6 ...{ 7 pub.Alert("数据库列表不能为空!", this.Page); 8 } 9 else if( txtInFile.Text =="" ) 10 ...{ 11 pub.Alert("备份文件名称不能为空!", this.Page); 12 13 } 14 else if( txtDbName.Text =="" || txtUserName.Text == "") 15 ...{ 16 pub.Alert("数据库,用户不能为空!!",this.Page); 17 18 } 19 else 20 ...{ 21 BtnDataRestore.Enabled = false; 22 BtnQuery.Enabled = false; 23 btnCear.Enabled = false; 24 if ( baks.BackUPDB(lstDb.SelectedItem.ToString(),txtInFile.Text,txtDbName.Text,txtUserName.Text,txtPwd.Text) == true ) 25 ...{ 26 pub.Alert("备份成功!",this.Page); 27 } 28 else 29 ...{ 30 pub.Alert("要备份的路径错误不存在,请输入正确路径!!",this.Page); 31 } 32 BtnDataRestore.Enabled = true; 33 BtnQuery.Enabled = true; 34 btnCear.Enabled = true; 35 } 36 } 37 38 private void BtnDataRestore_Click(object sender, System.EventArgs e) 39 ...{ 40 //恢复数据库 41 if ( lstDb.Items.Count == 0 ) 42 ...{ 43 pub.Alert("数据库列表不能为空!", this.Page); 44 } 45 else if ( txtOutFile.Text =="" ) 46 ...{ 47 pub.Alert("还原文件名称不能为空!", this.Page); 48 49 } 50 else if( txtDbName.Text =="" || txtUserName.Text == "") 51 ...{ 52 pub.Alert("数据库,用户不能为空!!",this.Page); 53 54 55 } 56 else 57 ...{ 58 BtnDataBackup.Enabled = false; 59 BtnQuery.Enabled = false; 60 btnCear.Enabled = false; 61 62 if ( baks.RestoreDB(lstDb.SelectedItem.ToString(),txtOutFile.Text,txtDbName.Text,txtUserName.Text,txtPwd.Text) == true ) 63 ...{ 64 pub.Alert("恢复成功!",this.Page); 65 } 66 else 67 ...{ 68 pub.Alert("要恢复的数据库文件不存在,请输入正确路径!!",this.Page); 69 } 70 BtnDataBackup.Enabled = true; 71 BtnQuery.Enabled = true; 72 btnCear.Enabled = true; 73 } 74 } 75 76 77 782. 79 80namespace WebSearch 81...{ 82 83 public class bakServer 84 ...{ 85 string ServerName; //数据服务器名称 86 string UserName; //用户名称 87 string Password; //用户密码 88 string message; //消息提示 89 90 public bakServer() 91 ...{ 92 // 93 // TODO: 在此处添加构造函数逻辑 94 // 95 } 96 /**//**//**//// 97 /**//// 取得数据库服务器列表 98 /**//// 99 /**//// 数据库服务器列表100 public ArrayList GetServerList() 101 ...{ 102 ArrayList alServers = new ArrayList() ; 103 SQLDMO.ApplicationClass sqlApp = new SQLDMO.ApplicationClass();104 try 105 ...{ 106 SQLDMO.NameList serverList = sqlApp.ListAvailableSQLServers() ; 107 if(serverList !=null)108 ...{109 for(int i = 1;i<= serverList.Count;i++) 110 ...{ 111 alServers.Add(serverList.Item(i)) ; 112 } 113 }114 else115 ...{116 message="你的系统需要打上SQL SERVER 2000 SP3这个补丁";117 }118 }119 catch(Exception e) 120 ...{ 121122 message = "取数据库服务器列表出错:" +e.Message;123124 } 125 finally 126 ...{ 127 sqlApp.Quit() ; 128 } 129 return alServers ; 130 }131 /**//**//**//// 132 /**//// 错误消息处理133 /**//// 134 /**//// 消息信息135 public string Msg()136 ...{ 137 return message;138 }139 /**//**//**//// 140 /**//// 取得指定数据库列表141 /**//// 142 /**//// 服务器名称143 /**//// 用户名称144 /**//// 用户密码145 /**//// 数据库列表146 public ArrayList GetDbList(string strServerName,string strUserName,string strPwd) 147 ...{ 148 ServerName = strServerName ; 149 UserName = strUserName ; 150 Password = strPwd ; 151 152 ArrayList alDbs = new ArrayList() ; 153 SQLDMO.ApplicationClass sqlApp = new SQLDMO.ApplicationClass() ; 154 SQLDMO.SQLServer svr = new SQLDMO.SQLServerClass() ; 155 try 156 ...{ 157 svr.Connect(ServerName,UserName,Password) ; 158 foreach(SQLDMO.Database db in svr.Databases) 159 ...{ 160 if(db.Name!=null) 161 alDbs.Add(db.Name) ; 162 } 163 } 164 catch(Exception err) 165 ...{ 166 // throw(new Exception("连接数据库出错:"+e.Message)) ; 167 message = "连接数据库出错:" +err.Message;168 } 169 finally 170 ...{ 171 svr.DisConnect() ; 172 sqlApp.Quit() ; 173 } 174 return alDbs ; 175 } 176 /**//**//**//// 177 /**//// 数据库名称178 /**//// 备份文件名179 /**//// 服务器名称180 /**//// 用户名称181 /**//// 密码182 /**//// 备份成功返回true ,否则返回false183 public bool BackUPDB(string strDbName,string strFileName, string strServerName,string strUserName,string strPwd) 184 ...{ 185 ServerName = strServerName ; 186 UserName = strUserName ; 187 Password = strPwd ; 188 SQLDMO.SQLServer svr = new SQLDMO.SQLServerClass() ; 189 try 190 ...{ 191 svr.Connect(ServerName,UserName,Password) ; 192 SQLDMO.Backup bak = new SQLDMO.BackupClass(); 193 bak.Action = 0 ; 194 bak.Initialize = true ; 195 SQLDMO.BackupSink_PercentCompleteEventHandler pceh = new SQLDMO.BackupSink_PercentCompleteEventHandler(Step); 196 bak.PercentComplete += pceh; 197 bak.Files = strFileName; 198 bak.Database = strDbName; 199 bak.SQLBackup(svr); 200 return true ; 201 } 202 catch(Exception err) 203 ...{ 204 // throw(new Exception("备份数据库失败"+err.Message)) ; 205 message = "备份数据库失败:" +err.Message;206 return false ; 207 } 208 finally 209 ...{ 210 svr.DisConnect() ; 211 } 212 } 213214 /**//**//**//// 备份文件名215 /**//// 状态条控件名称216 /**//// 服务器名称217 /**//// 用户名称218 /**//// 密码219 /**//// 恢复成功返回true ,否则返回false220 public bool RestoreDB(string strDbName,string strFileName,string strServerName,string strUserName,string strPwd ) 221 ...{ 222 SQLDMO.SQLServer svr = new SQLDMO.SQLServerClass() ; 223 try 224 ...{ 225 ServerName = strServerName ; 226 UserName = strUserName ; 227 Password = strPwd ; 228229 svr.Connect(ServerName,UserName,Password) ; 230 SQLDMO.QueryResults qr = svr.EnumProcesses(-1) ; 231 int iColPIDNum = -1 ; 232 int iColDbName = -1 ; 233 for(int i=1;i<=qr.Columns;i++) 234 ...{ 235 string strName = qr.get_ColumnName(i) ; 236 if (strName.ToUpper().Trim() == "SPID") 237 ...{ 238 iColPIDNum = i ; 239 } 240 else if (strName.ToUpper().Trim() == "DBNAME") 241 ...{ 242 iColDbName = i ; 243 } 244 if (iColPIDNum != -1 && iColDbName != -1) 245 break ; 246 } 247248 for(int i=1;i<=qr.Rows;i++) 249 ...{ 250 int lPID = qr.GetColumnLong(i,iColPIDNum) ; 251 string strDBName = qr.GetColumnString(i,iColDbName) ; 252 if (strDBName.ToUpper() == strDbName.ToUpper()) 253 svr.KillProcess(lPID) ; 254 } 255256 SQLDMO.Restore res = new SQLDMO.RestoreClass() ; 257 res.Action = 0 ; 258 SQLDMO.RestoreSink_PercentCompleteEventHandler pceh = new SQLDMO.RestoreSink_PercentCompleteEventHandler(Step); 259 res.PercentComplete += pceh; 260 res.Files = strFileName ; 261262 res.Database = strDbName ; 263 res.ReplaceDatabase = true ; 264 res.SQLRestore(svr) ; 265 return true ; 266 } 267 catch(Exception err) 268 ...{ 269270 message = "恢复数据库失败,请关闭所有和该数据库连接的程序!" +err.Message;271 return false;272 } 273 finally 274 ...{ 275 svr.DisConnect() ; 276 } 277 } 278 279 }280}281