/// <summary>
/// 通过调用MSSQL的SQLDMO.DLL文件来实现备份数据库
/// 1.首先在在项目中引用SQLDMO.DLL文件。
/// 2.在引用中的SQLDMO.DLL文件右击-->属性-->设置[嵌入互操作类型]为flash
/// </summary>
public static void DBBackup()
{
SQLDMO.Backup dbBackup = new SQLDMO.BackupClass();
SQLDMO.SQLServer sqlServer = new SQLDMO.SQLServerClass();
try
{
sqlServer.LoginSecure = false;
sqlServer.Connect("localhost", "sa", "");
dbBackup.Action = SQLDMO.SQLDMO_BACKUP_TYPE.SQLDMOBackup_Database;
dbBackup.Database = "kuang"; //数据库名字
dbBackup.Files = @"d:\kuang_bak.bak"; //备份位置
dbBackup.BackupSetName = "kuang"; //名称,在企业管理器里面选择备份的时候也有这个,貌似没用。
dbBackup.BackupSetDescription = "数据库备份"; //描述,在企业管理器里面选择备份的时候也有这个,貌似没用。
dbBackup.Initialize = true;
dbBackup.SQLBackup(sqlServer);
}
catch
{
throw;
}
finally
{
sqlServer.DisConnect();
}
}
/// <summary>
/// 通过调用MSSQL的SQLDMO.DLL文件来实现从备份文件恢复到是数据库
/// 注:恢复是数据库必须是没有链接的。清楚链接的方法:在企业管理器右击数据库-->分离数据库-->点击清楚按钮即可
/// </summary>
private void DBReply()
{
SQLDMO.Restore restore = new SQLDMO.RestoreClass();
SQLDMO.SQLServer sqlserver = new SQLDMO.SQLServerClass();
sqlserver.LoginSecure = false;
sqlserver.Connect("localhost", "sa", "");
restore.Action = SQLDMO.SQLDMO_RESTORE_TYPE.SQLDMORestore_Database;
restore.Database = "kuang";
restore.Files = @"d:\kuang_bak.bak";
restore.FileNumber = 1;
restore.SQLRestore(sqlserver);
}
在回复的过程中,需要保证数据库没有正在使用,网上看到的代码经测试没有效果,如果有朋友知道方法的恳请您回复一下,谢谢。
代码是网络上看到的,测试整理了一下,感谢原作者。
-------------------------------------------------------------------------------------------
- C#实现SQLSERVER2000数据库备份还原的两种方法
- :方法一(不使用SQLDMO):
- ///
- ///备份方法
- ///
- SqlConnectionconn=newSqlConnection("Server=.;Database=master;UserID=sa;Password=sa;");
- SqlCommandcmdBK=newSqlCommand();
- cmdBK.CommandType=CommandType.Text;
- cmdBK.Connection=conn;
- cmdBK.CommandText=@"backupdatabasetesttodisk='C:/ba'withinit";
- try
- {
- conn.Open();
- cmdBK.ExecuteNonQuery();
- MessageBox.Show("Backupsuccessed.");
- }
- catch(Exceptionex)
- {
- MessageBox.Show(ex.Message);
- }
- finally
- {
- conn.Close();
- conn.Dispose();
- }
- ///
- ///还原方法
- ///
- SqlConnectionconn=newSqlConnection("Server=.;Database=master;UserID=sa;Password=sa;Trusted_Connection=False");
- conn.Open();
- //KILLDataBaseProcess
- SqlCommandcmd=newSqlCommand("SELECTspidFROMsysprocesses,sysdatabasesWHEREsysprocesses.dbid=sysdatabases.dbidANDsysdatabases.Name='test'",conn);
- SqlDataReaderdr;
- dr=cmd.ExecuteReader();
- ArrayListlist=newArrayList();
- while(dr.Read())
- {
- list.Add(dr.GetInt16(0));
- }
- dr.Close();
- for(inti=0;i<list.Count;i++)
- {
- cmd=newSqlCommand(string.Format("KILL{0}",list),conn);
- cmd.ExecuteNonQuery();
- }
- SqlCommandcmdRT=newSqlCommand();
- cmdRT.CommandType=CommandType.Text;
- cmdRT.Connection=conn;
- cmdRT.CommandText=@"restoredatabasetestfromdisk='C:/ba'";
- try
- {
- cmdRT.ExecuteNonQuery();
- MessageBox.Show("Restoresuccessed.");
- }
- catch(Exceptionex)
- {
- MessageBox.Show(ex.Message);
- }
- finally
- {
- conn.Close();
- }
- 方法二(使用SQLDMO):
- ///
- ///备份方法
- ///
- SQLDMO.Backupbackup=newSQLDMO.BackupClass();
- SQLDMO.SQLServerserver=newSQLDMO.SQLServerClass();
- //显示进度条
- SQLDMO.BackupSink_PercentCompleteEventHandlerprogress=newSQLDMO.BackupSink_PercentCompleteEventHandler(Step);
- backup.PercentComplete+=progress;
- try
- {
- server.LoginSecure=false;
- server.Connect(".","sa","sa");
- backup.Action=SQLDMO.SQLDMO_BACKUP_TYPE.SQLDMOBackup_Database;
- backup.Database="test";
- backup.Files=@"D:/test/myProg/backupTest";
- backup.BackupSetName="test";
- backup.BackupSetDescription="Backupthedatabaseoftest";
- backup.Initialize=true;
- backup.SQLBackup(server);
- MessageBox.Show("Backupsuccessed.");
- }
- catch(Exceptionex)
- {
- MessageBox.Show(ex.Message);
- }
- finally
- {
- server.DisConnect();
- }
- this.pbDB.Value=0;
- ///
- ///还原方法
- ///
- SQLDMO.Restorerestore=newSQLDMO.RestoreClass();
- SQLDMO.SQLServerserver=newSQLDMO.SQLServerClass();
- //显示进度条
- SQLDMO.RestoreSink_PercentCompleteEventHandlerprogress=newSQLDMO.RestoreSink_PercentCompleteEventHandler(Step);
- restore.PercentComplete+=progress;
- //KILLDataBaseProcess
- SqlConnectionconn=newSqlConnection("Server=.;Database=master;UserID=sa;Password=sa;Trusted_Connection=False");
- conn.Open();
- SqlCommandcmd=newSqlCommand("SELECTspidFROMsysprocesses,sysdatabasesWHEREsysprocesses.dbid=sysdatabases.dbidANDsysdatabases.Name='test'",conn);
- SqlDataReaderdr;
- dr=cmd.ExecuteReader();
- ArrayListlist=newArrayList();
- while(dr.Read())
- {
- list.Add(dr.GetInt16(0));
- }
- dr.Close();
- for(inti=0;i<list.Count;i++)
- {
- cmd=newSqlCommand(string.Format("KILL{0}",list),conn);
- cmd.ExecuteNonQuery();
- }
- conn.Close();
- try
- {
- server.LoginSecure=false;
- server.Connect(".","sa","sa");
- restore.Action=SQLDMO.SQLDMO_RESTORE_TYPE.SQLDMORestore_Database;
- restore.Database="test";
- restore.Files=@"D:/test/myProg/backupTest";
- restore.FileNumber=1;
- restore.ReplaceDatabase=true;
- restore.SQLRestore(server);
- MessageBox.Show("Restoresuccessed.");
- }
- catch(Exceptionex)
- {
- MessageBox.Show(ex.Message);
- }
- finally
- {
- server.DisConnect();
- }
- this.pbDB.Value=0;
SQLDMO(SQL Distributed Management Objects,SQL分布式管理对象)封装 Microsoft SQL Server 2000 数据库中的对象。SQL-DMO 允许用支持自动化或 COM 的语言编写应用程序,以管理 SQL Server 安装的所有部分。SQL-DMO 是 SQL Server 2000 中的 SQL Server 企业管理器所使用的应用程序接口 (API);因此使用 SQL-DMO 的应用程序可以执行 SQL Server 企业管理器执行的所有功能。
SQL-DMO 用于必须包含 SQL Server 管理的任何自动化或 COM 应用程序,例如:
1.封装 SQL Server 作为自己的数据存储并想尽量减少用户的 SQL Server 管理任务的应用程序。
2.在程序本身并入了专门的管理逻辑的应用程序。
3.想在自己的用户界面中集成 SQL Server 管理任务的应用程序。
SQLDMO对象来自SQLDMO.dll,SQLDMO.dll是随SQL Server2000一起发布的。SQLDMO.dll自身是一个COM对象,因此,在你的.NET项目里必须先引用它。
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)