Net下的資料備份和還原

來源:互聯網
上載者:User
備份|資料 資料庫:SQL Server ;引用SQLDMO
◆資料備份 :
/// <summary>
/// 備份資料庫
/// </summary>
private void btnBackUp_Click(object sender, System.EventArgs e)
{
this.Cursor = Cursors.WaitCursor;
this.label1.Text = " 進行中檔案庫的資料備份,這可能需要幾秒到幾十的時間,請稍候...";
this.label1.Visible = true;
this.label1.Refresh();
this.pBar1.Visible = true;
//------------------------------------------------------------------------------------


string selfName = “D:\NorthwindBak“;
string deviceName = “NorthwindBak“;
string remark = "備份測試";

//◆資料備份:
SQLDMO.Backup oBackup = new SQLDMO.BackupClass();
SQLDMO.SQLServer oSQLServer = new SQLDMO.SQLServerClass();
oBackup.Action = 0 ;
oBackup.Initialize = true ;
SQLDMO.BackupSink_PercentCompleteEventHandler pceh = new SQLDMO.BackupSink_PercentCompleteEventHandler(Step);
oBackup.PercentComplete += pceh;

try
{
oSQLServer.LoginSecure = false;
oSQLServer.Connect(Common.MySettings.SqlServerName, "sa", "");
oBackup.Action = SQLDMO.SQLDMO_BACKUP_TYPE.SQLDMOBackup_Database;
oBackup.Database = "Northwind";//資料庫名
oBackup.Files = selfName;//檔案路徑
oBackup.BackupSetName = deviceName;//備份名稱
oBackup.BackupSetDescription = remark;//備份描述
oBackup.Initialize = true;
oBackup.SQLBackup(oSQLServer);

}
catch(System.Exception ex)
{
Common.ShowMsg("資料備份失敗:\n" + ex.ToString());
}
finally
{
oSQLServer.DisConnect();
}

//------------------------------------------------------------------------------------
this.label1.Visible = false;
this.pBar1.Visible = false;
this.Cursor = Cursors.Default;
}


/// <summary>
/// 顯示備份進度條
/// </summary>
private void Step(string message,int percent)
{
this.pBar1.Value = percent ;
}

◆資料還原 :
/// <summary>
/// 還原資料庫
/// </summary>
private void btnRestore_Click(object sender, System.EventArgs e)
{

this.Cursor = Cursors.WaitCursor;
this.label1.Text = " 進行中檔案庫的資料還原,這可能需要幾秒到幾十的時間,請稍候...";
this.label1.Visible = true;
this.label1.Refresh();
this.pBar1.Visible = true;
//------------------------------------------------------------------------------------

string fileName = "NorthwindBak";
string filePath = "D:\NorthwindBak";
string remark = "備份測試";

SQLDMO.Restore oRestore = new SQLDMO.RestoreClass();
SQLDMO.SQLServer oSQLServer = new SQLDMO.SQLServerClass();
oRestore.Action = 0 ;
SQLDMO.RestoreSink_PercentCompleteEventHandler pceh = new SQLDMO.RestoreSink_PercentCompleteEventHandler(Step);
oRestore.PercentComplete += pceh;
try
{
oSQLServer.Connect(Common.MySettings.SqlServerName, "sa", "");
SQLDMO.QueryResults qr = oSQLServer.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() == "CgRecord".ToUpper())
oSQLServer.KillProcess(lPID) ;
}


oRestore.Action = SQLDMO.SQLDMO_RESTORE_TYPE.SQLDMORestore_Database;
oRestore.Database = "Northwind";
oRestore.Files = filePath;
oRestore.FileNumber = 1;
oRestore.ReplaceDatabase = true;
oRestore.SQLRestore(oSQLServer);


}
catch(System.Exception ex)
{
Common.ShowMsg("資料還原失敗:\n" + ex.ToString());
}
finally
{
oSQLServer.DisConnect();
}

//------------------------------------------------------------------------------------
this.label1.Visible = false;
this.pBar1.Visible = false;
this.Cursor = Cursors.Default;
}

/// <summary>
/// 顯示還原進度條
/// </summary>
private void Step(string message,int percent)
{
this.pBar1.Value = percent ;
}




相關文章

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

11.11 Big Sale for Cloud

Get Unbeatable Offers with up to 90% Off,Oct.24-Nov.13 (UTC+8)

Get It Now >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。