首先下载SQLDMO文件包,下载地址:SQLDMO
下载后,进行解压,然后按照下面的步骤进行操作:
第一步:首先将msvcr71.dll, SQLDMO.DLL, Resources/2052/sqldmo.rll,Resources/1033/sqldmo.rll 拷贝到C:/Program Files/Microsoft SQL Server/80/Tools/Binn目录。
第二步:打开开始,在运行中输入 regsvr32 "C:/Program Files/Microsoft SQL Server/80/Tools/Binn/sqldmo.dll" 注册sqldmo.dll文件。
如果经过以上两次操作后,访问依然提示如下错误:
说明C:/Program Files/文件夹仅有Administrator和System的控制权限,而没有其他任何用户的权限,因此我们为Microsoft SQL Server文件夹增加上Network Service 的读取权限。
即使进行注册后,运行程序中还会报错,类似如下信息:
“event invocation for COM objects requires event to be attributed with DispIdAttribute”
这个原因是在项目Bin中引用SQLDMO.dll文件后,该dll属性中的“嵌入互操作类型”的值默认是True造成的,修改成False即可。
下面附上数据库备份还原操作的主要代码:
----------------------------------数据备份------------------------------------------------
private void btnSave_Click(object sender, RoutedEventArgs e) { //备份 SaveFileDialog saveFileDialog = new SaveFileDialog(); saveFileDialog.Filter = "(*.mdf)|*.mdf"; saveFileDialog.FilterIndex = 0; if (saveFileDialog.ShowDialog() == true) { string filePath = saveFileDialog.FileName; SQLBACK(Global.DB_SOURCE, Global.DB_UID, Global.DB_UPWD, Global.DB_NAME, filePath); } } #region SQL数据库备份函数 /// < summary> /// SQL数据库备份 /// < /summary> /// < param name="ServerIP">SQL服务器IP或(Localhost)< /param> /// < param name="LoginUserName">数据库登录名< /param> /// < param name="LoginPass">数据库登录密码< /param> /// < param name="DBName">数据库名< /param> /// < param name="BackPath">备份到的路径< /param> public void SQLBACK(string ServerIP, string LoginUserName, string LoginPass, string DBName, string BackPath) { SQLDMO.Backup oBackup = new SQLDMO.Backup(); SQLDMO.SQLServer oSQLServer = new SQLDMO.SQLServer(); oBackup.Action = 0; oBackup.Initialize = true; SQLDMO.BackupSink_PercentCompleteEventHandler pceh = new SQLDMO.BackupSink_PercentCompleteEventHandler(Step); oBackup.PercentComplete += pceh; try { oSQLServer.LoginSecure = false; oSQLServer.Connect(ServerIP, LoginUserName, LoginPass); oBackup.Action = SQLDMO.SQLDMO_BACKUP_TYPE.SQLDMOBackup_Database; oBackup.Database = DBName; oBackup.Files = @"" + BackPath + ""; oBackup.BackupSetName = DBName; oBackup.BackupSetDescription = "数据库备份"; oBackup.Initialize = true; oBackup.SQLBackup(oSQLServer); MessageBox.Show("备份成功!"); } catch (Exception ex) { MessageBox.Show("备份失败,原因为:" + ex.Message); } finally { oSQLServer.DisConnect(); } } private void Step(string message, int percent) { this.pb.Value = percent; }
--------------------------------数据还原----------------------------------------------------------
private static DataRestore _Instance = null; //页面实例 public DataRestore() { InitializeComponent(); } public static DataRestore Instance() { if (_Instance == null) { _Instance = new DataRestore(); } else { MessageBox.Show("已经有一个实例在运行!"); } return _Instance; } /// <summary> /// 获取数据库服务器列表 /// </summary> private void GetSQLServerList() { SQLDMO._Application sqlApp = new SQLDMO.Application(); SQLDMO.NameList sqlServers = sqlApp.ListAvailableSQLServers(); for (int i = 0; i < sqlServers.Count; i++) { object srv = sqlServers.Item(i + 1); if (srv != null) { // this.cboServers.Items.Add(srv); } } //if (this.cboServers.Items.Count > 0) // this.cboServers.SelectedIndex = 0; //else // this.cboServers.Text = "<No available SQL Servers>"; } private void btnSave_Click(object sender, RoutedEventArgs e) { //还原 OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.Filter = "(*.mdf)|*.mdf"; openFileDialog.FilterIndex = 0; if (openFileDialog.ShowDialog() == true) { string filePath = openFileDialog.FileName; //获取选中的备份文件,进行还原操作 SQLDbRestore(Global.DB_SOURCE, Global.DB_UID, Global.DB_UPWD, Global.DB_NAME, filePath); } } #region SQL恢复数据库 /// < summary> /// SQL恢复数据库 /// < /summary> /// < param name="ServerIP">SQL服务器IP或(Localhost)< /param> /// < param name="LoginUserName">数据库登录名< /param> /// < param name="LoginPass">数据库登录密码< /param> /// < param name="DBName">要还原的数据库名< /param> /// < param name="BackPath">数据库备份的路径< /param> public void SQLDbRestore(string ServerIP, string LoginUserName, string LoginPass, string DBName, string BackPath) { SQLDMO.Restore oRestore = new SQLDMO.Restore(); SQLDMO.SQLServer oSQLServer = new SQLDMO.SQLServer(); oRestore.Action = 0; //SQLDMO.RestoreSink_PercentCompleteEventHandler pceh = new SQLDMO.RestoreSink_PercentCompleteEventHandler(Step); //oRestore.PercentComplete += pceh; try { oSQLServer.Connect(ServerIP, LoginUserName, LoginPass); SQLDMO.QueryResults qr = oSQLServer.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() == "JCWZDB".ToUpper()) { oSQLServer.KillProcess(lPID); } } oRestore.Action = SQLDMO.SQLDMO_RESTORE_TYPE.SQLDMORestore_Database; oRestore.Database = DBName; oRestore.Files = BackPath; oRestore.FileNumber = 1; oRestore.ReplaceDatabase = true; oRestore.SQLRestore(oSQLServer); MessageBox.Show("数据还原成功!"); } catch (System.Exception ex) { MessageBox.Show("数据还原失败: " + ex.ToString()); } finally { oSQLServer.DisConnect(); } } private void Step(string message, int percent) { this.pb.Value = percent; } #endregion