asp.net下实现数据库的备份与恢复

本文介绍了一个基于 SQL Server 的数据库备份与恢复系统实现方法。通过 C# 代码示例展示了如何进行数据库的备份与恢复操作,并包含了错误处理及进度通知等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

 11.
  
2
  3
private 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
 80
namespace 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    else
115    {
116       message="你的系统需要打上SQL SERVER 2000 SP3这个补丁";
117    }

118   }

119   catch(Exception e) 
120   
121
122    message = "取数据库服务器列表出错:" +e.Message;
123
124   }
 
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 ,否则返回false
183  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  }
 
213
214  /**//// 备份文件名
215  /// 状态条控件名称
216  /// 服务器名称
217  /// 用户名称
218  /// 密码
219  /// 恢复成功返回true ,否则返回false
220  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 ; 
228
229    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    }
 
247
248    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    }
 
255
256    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 ; 
261
262    res.Database = strDbName ; 
263    res.ReplaceDatabase = true ; 
264    res.SQLRestore(svr) ; 
265    return true ; 
266   }
 
267   catch(Exception err) 
268   
269
270    message = "恢复数据库失败,请关闭所有和该数据库连接的程序!" +err.Message;
271    return false;
272   }
 
273   finally 
274   
275    svr.DisConnect() ; 
276   }
 
277  }
 
278       
279 }

280}

281
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值