http://winzheng.blog.ccidnet.com/blog/ccid/do_showone/tid_9268.html
asp.net下实现数据库的备份与恢复 作者: winzheng 发表日期: 2005-09-05 15:07 文章属性: 原创 复制链接 |
WebDataBak.aspx.cs bakServer.cs为自定义类代码 核心代码: 1. private void BtnDataBackup_Click(object sender, System.EventArgs e) } } private void BtnDataRestore_Click(object sender, System.EventArgs e)
if ( baks.RestoreDB(lstDb.SelectedItem.ToString(),txtOutFile.Text,txtDbName.Text,txtUserName.Text,txtPwd.Text) == true ) 2. namespace WebSearch /*-------------------------------------------------------------------------- 系统名称: xxxxx 设计时间:2005-09-03 /// 取得数据库服务器列表 /// /// 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; } /// 错误消息处理 /// /// 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 ; } /// /// 数据库名称 /// 备份文件名 /// 服务器名称 /// 用户名称 /// 密码 /// 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() ; } } /// 备份文件名 svr.Connect(ServerName,UserName,Password) ; for(int i=1;i<=qr.Rows;i++) SQLDMO.Restore res = new SQLDMO.RestoreClass() ; res.Database = strDbName ; message = "恢复数据库失败,请关闭所有和该数据库连接的程序!" +err.Message; |
posted on 2006-02-05 15:05 在学习.net的道路上 阅读(258) 评论(0) 编辑 收藏 举报