C# 備份與恢復(fù)SQL Server數(shù)據(jù)庫代碼
發(fā)表時間:2023-07-26 來源:明輝站整理相關(guān)軟件相關(guān)文章人氣:
[摘要]軟件等級:更新時間:2016-11-11版本號:v5.7.10 MySQL Server x64官方正式版免費下載立即下載 C# 備份和恢復(fù)SQL Server數(shù)據(jù)庫代碼 QLDMO(S...
C# 備份和恢復(fù)SQL Server數(shù)據(jù)庫代碼
QLDMO(SQL Distributed Management Objects,SQL分布式管理對象)封裝了Microsoft SQL Server數(shù)據(jù)庫中的對象。SQLDMO是Microsoft SQL Server中企業(yè)管理器所使用的應(yīng)用程序接口,所以它可以執(zhí)行很多功能,其中當(dāng)然也包括對數(shù)據(jù)庫的備份和恢復(fù)。
SQLDMO由Microsoft SQL Server自帶的SQLDMO.dll提供,由于SQLDMO.dll是一個COM對象(有關(guān)COM的概念,請參看我的COM與COM+技術(shù)那篇文章),所以大家在用之前必須在.Net項目中添加對它的引用。
下面是用C#語言書寫的用于Microsoft SQL Server數(shù)據(jù)庫備份和恢復(fù)的類:
public sealed class DbOper
{
///
/// DbOper類的構(gòu)造函數(shù)
///作者:Shadow
///
private DbOper()
{
}
///
/// 數(shù)據(jù)庫備份
///
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 = "數(shù)據(jù)庫備份
";
oBackup.Initialize = true;
oBackup.SQLBackup(oSQLServer);
}
catch
{
throw;
}
finally
{
oSQLServer.DisConnect();
}
}
///
/// 數(shù)據(jù)庫恢復(fù)
///
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();
}
}
}
學(xué)習(xí)教程快速掌握從入門到精通的電腦知識