sqlserver備份還原數(shù)據(jù)庫(kù)功能封裝分享
using System;
using SQLDMO;
namespace EDTBackupTool
{
/// <summary>
/// Backup 的摘要說(shuō)明。
/// </summary>
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 = "數(shù)據(jù)庫(kù)備份";
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<=qr.Columns;i++)
{
string strName = qr.get_ColumnName(i) ;
if (strName.ToUpper().Trim() == "SPID")
{
iColPIDNum = i ;
}
else if (strName.ToUpper().Trim() == ClientCommon.CommonFunctions.Catalog)
{
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() == ClientCommon.CommonFunctions.Catalog)
{
svr.KillProcess(lPID) ;
}
}
SQLDMO.Restore res = new SQLDMO.RestoreClass() ;
res.Action = 0 ;
res.Files = strFileName ;
res.Database = ClientCommon.CommonFunctions.Catalog ;
res.ReplaceDatabase = true ;
res.SQLRestore(svr) ;
return true ;
}
catch
{
return false;
}
finally
{
svr.DisConnect() ;
}
}
}
}
相關(guān)文章
C#日期格式字符串的相互轉(zhuǎn)換操作實(shí)例分析
這篇文章主要介紹了C#日期格式字符串的相互轉(zhuǎn)換操作,結(jié)合實(shí)例形式分析了C#日期格式字符串的相互轉(zhuǎn)換操作函數(shù)與相關(guān)使用技巧,需要的朋友可以參考下2019-08-08C#實(shí)現(xiàn)合并多張圖片為GIF動(dòng)態(tài)圖
這篇文章主要為大家詳細(xì)介紹了C#如何將把一張又一張的圖片去拼合成一張GIF動(dòng)態(tài)圖片,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2022-12-12數(shù)字金額大寫(xiě)轉(zhuǎn)換器制作代碼分享(人民幣大寫(xiě)轉(zhuǎn)換)
一個(gè)人民幣大寫(xiě)的擴(kuò)展方法,可以做成數(shù)字金額大寫(xiě)轉(zhuǎn)換器,大家參考使用吧2013-12-12C# ComboBox的聯(lián)動(dòng)操作(三層架構(gòu))
這篇文章主要介紹了C# ComboBox的聯(lián)動(dòng)操作(三層架構(gòu)),根據(jù)下拉框的變化使得下拉框綁定對(duì)應(yīng)值,感興趣的小伙伴們可以參考一下2016-05-05C#編程報(bào)錯(cuò)System.InvalidOperationException問(wèn)題及解決
這篇文章主要介紹了C#編程報(bào)錯(cuò)System.InvalidOperationException問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-05-05