sqlserver备份还原数据库功能封装分享
本文导语: 代码如下:using System;using SQLDMO; namespace EDTBackupTool{ /// /// Backup 的摘要说明。 /// public class SQLTools { public static bool Backup(string backfile){ try { SQLDMO.Backup backup = new BackupClass(); SQLDMO.SQLServer server = new SQLServerCl...
using System;
using SQLDMO;
namespace EDTBackupTool
{
///
/// Backup 的摘要说明。
///
public class SQLTools
{
public static bool Backup(string backfile){
try
{
SQLDMO.Backup backup = new BackupClass();
SQLDMO.SQLServer server = new SQLServerClass();
server.LoginSecure = true;
server.Connect(ClientCommon.CommonFunctions.DataSource,"sa","");
backup.Action = SQLDMO.SQLDMO_BACKUP_TYPE.SQLDMOBackup_Database;
backup.Database = ClientCommon.CommonFunctions.Catalog;
backup.Files =backfile;
backup.BackupSetName =ClientCommon.CommonFunctions.Catalog;
backup.BackupSetDescription = "数据库备份";
backup.Initialize = true;
backup.SQLBackup(server);
return true;
}
catch(Exception ex){
ClientCommon.ExceptionRecorder.Record(ex);
return false;
}
}
public static bool RestoreDB(string strFileName)
{
SQLDMO.SQLServer svr = new SQLDMO.SQLServerClass() ;
try
{
svr.LoginSecure=true;
svr.Connect(ClientCommon.CommonFunctions.DataSource,"sa","") ;
SQLDMO.QueryResults qr = svr.EnumProcesses(-1) ;
int iColPIDNum = -1 ;
int iColDbName = 1 ;
for(int i=1;i