在C#中运用SQLDMO备份和恢复SQL Server数据库(转)
在C#中运用SQLDMO备份和恢复SQL Server数据库
SQLDMO(SQL Distributed Management Objects,SQL分布式管理对象)封装了Microsoft SQL Server数据库中的对象。SQLDMO是Microsoft SQL Server中企业管理器所使用的应用程序接口,所以它可以执行很多功能,其中当然也包括对数据库的备份和恢复。
SQLDMO由Microsoft SQL Server自带的SQLDMO.dll提供,由于SQLDMO.dll是一个COM对象(有关COM的概念,请参看我的COM与COM+技术那篇文章),所以大家在用之前必须在.NET项目中添加对它的引用。
SQLDMO由Microsoft SQL Server自带的SQLDMO.dll提供,由于SQLDMO.dll是一个COM对象(有关COM的概念,请参看我的COM与COM+技术那篇文章),所以大家在用之前必须在.NET项目中添加对它的引用。
下面是用C#语言书写的用于Microsoft SQL Server数据库备份和恢复的类:
public sealed class DbOper
{
///<summary>
/// DbOper类的构造函数
///作者:Shadow
///</summary>
private DbOper()
{
}
///<summary>
/// 数据库备份
///</summary>
public static void DbBackup()
{
SQLDMO.Backup oBackup = new SQLDMO.BackupClass();
SQLDMO.SQLServer oSQLServer = new SQLDMO.SQLServerClass();
try
{
oSQLServer.LoginSecure = false;
oSQLServer.Connect("localhost", "sa", "1234");
oBackup.Action = SQLDMO.SQLDMO_BACKUP_TYPE.SQLDMOBackup_Database;
oBackup.Database = "Northwind";
oBackup.Files = @"d:\Northwind.bak";
oBackup.BackupSetName = "Northwind";
oBackup.BackupSetDescription = "数据库备份";
oBackup.Initialize = true;
oBackup.SQLBackup(oSQLServer);
}
catch
{
throw;
}
finally
{
oSQLServer.DisConnect();
}
}
其中这个语句取得了所有的进程列表:
SQLDMO.QueryResults qr = svr.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() == strDbName.ToUpper())
svr.KillProcess(lPID) ;
}
SQLDMO.QueryResults qr = svr.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() == strDbName.ToUpper())
svr.KillProcess(lPID) ;
}
///<summary>
/// 数据库恢复
///</summary>
public static void DbRestore()
{
SQLDMO.Restore oRestore = new SQLDMO.RestoreClass();
SQLDMO.SQLServer oSQLServer = new SQLDMO.SQLServerClass();
try
{
oSQLServer.LoginSecure = false;
oSQLServer.Connect("localhost", "sa", "1234");
oRestore.Action = SQLDMO.SQLDMO_RESTORE_TYPE.SQLDMORestore_Database;
oRestore.Database = "Northwind";
oRestore.Files = @"d:\Northwind.bak";
oRestore.FileNumber = 1;
oRestore.ReplaceDatabase = true;
oRestore.SQLRestore(oSQLServer);
}
catch
{
throw;
}
finally
{
oSQLServer.DisConnect();
}
}
}
注册SQLDMO.dll用到的组件
SQLDMO.DLL
SQLDMO.rll
sqlresld.dl
sqlsvc.dll
sqlsvc.rll
Sqlwid.dll
Sqlwoa.dll
w95scm.dll
SQLDMO.DLL
SQLDMO.rll
sqlresld.dl
sqlsvc.dll
sqlsvc.rll
Sqlwid.dll
Sqlwoa.dll
w95scm.dll
注:如果注册不成功请更新MDAC版本至2.8
本文来自博客园,作者:古道轻风,转载请注明原文链接:https://www.cnblogs.com/88223100/archive/2008/04/28/1174658.html