在ASP.NET頁面中實現Database Backup和恢複功能
最後更新:2017-02-28
來源:互聯網
上載者:User
asp.net|備份|恢複|資料|資料庫|頁面 using System;
using SQLDMO;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace bakServe040907
{
/// <summary>
/// Database Backup類說明
/// </summary>
public class bakServer
{
string ServerName; //資料服務器名稱
string UserName; //使用者名稱稱
string Password; //使用者密碼
string message; //訊息提示
ProgressBar PBar; //狀態條控制項
public bakServer()
{
}
/// <summary>
/// 取得資料庫伺服器列表
/// </summary>
/// <returns>資料庫伺服器列表</returns>
public ArrayList GetServerList()
{
ArrayList alServers = new ArrayList() ;
SQLDMO.Application sqlApp = new SQLDMO.ApplicationClass() ;
try
{
SQLDMO.NameList serverList = sqlApp.ListAvailableSQLServers() ;
for(int i = 1;i<= serverList.Count;i++)
{
alServers.Add(serverList.Item(i)) ;
}
}
catch(Exception e)
{
// throw(new Exception("取資料庫伺服器列表出錯:"+e.Message)) ;
message = "取資料庫伺服器列表出錯:" +e.Message;
}
finally
{
sqlApp.Quit() ;
}
return alServers ;
}
/// <summary>
/// 錯誤訊息處理
/// </summary>
/// <returns>訊息資訊</returns>
public string Msg()
{
return message;
}
/// <summary>
/// 取得指定資料庫列表
/// </summary>
/// <param name="strServerName">伺服器名稱</param>
/// <param name="strUserName">使用者名稱稱</param>
/// <param name="strPwd">使用者密碼</param>
/// <returns>資料庫列表</returns>
public ArrayList GetDbList(string strServerName,string strUserName,string strPwd)
{
ServerName = strServerName ;
UserName = strUserName ;
Password = strPwd ;
ArrayList alDbs = new ArrayList() ;
SQLDMO.Application sqlApp = new SQLDMO.ApplicationClass() ;
SQLDMO.SQLServer svr = new SQLDMO.SQLServerClass() ;
try
{
svr.Connect(ServerName,UserName,Password) ;
foreach(SQLDMO.Database db in svr.Databases)
{
if(db.Name!=null)
alDbs.Add(db.Name) ;
}
}
catch(Exception err)
{
// throw(new Exception("串連資料庫出錯:"+e.Message)) ;
message = "串連資料庫出錯:" +err.Message;
}
finally
{
svr.DisConnect() ;
sqlApp.Quit() ;
}
return alDbs ;
}
//資料庫的備份和即時進度顯示代碼:
/// <summary>
/// 資料庫的備份和即時進度顯示
/// </summary>
/// <param name="strDbName">資料庫名稱</param>
/// <param name="strFileName">備份檔案名</param>
/// <param name="pgbMain">狀態條控制項名稱</param>
/// <param name="strServerName">伺服器名稱</param>
/// <param name="strUserName">使用者名稱稱</param>
/// <param name="strPwd">密碼</param>
/// <returns>備份成功返回true ,否則返回false</returns>
public bool BackUPDB(string strDbName,string strFileName, ProgressBar pgbMain,string strServerName,string strUserName,string strPwd)
{
ServerName = strServerName ;
UserName = strUserName ;
Password = strPwd ;
PBar = pgbMain;
SQLDMO.SQLServer svr = new SQLDMO.SQLServerClass() ;
try
{
svr.Connect(ServerName,UserName,Password) ;
SQLDMO.Backup bak = new SQLDMO.BackupClass();
bak.Action = 0 ;
bak.Initialize = true ;
SQLDMO.BackupSink_PercentCompleteEventHandler pceh = new SQLDMO.BackupSink_PercentCompleteEventHandler(Step);
bak.PercentComplete += pceh;
bak.Files = strFileName;
bak.Database = strDbName;
bak.SQLBackup(svr);
return true ;
}
catch(Exception err)
{
// throw(new Exception("備份資料庫失敗"+err.Message)) ;
message = "備份資料庫失敗:" +err.Message;
return false ;
}
finally
{
svr.DisConnect() ;
}
}
/// <summary>
/// 顯示進度條的當前進度
/// </summary>
/// <param name="message"></param>
/// <param name="percent"></param>
private void Step(string message,int percent)
{
PBar.Value = percent ;
}
// 其中,這兩個語句實現了進度的即時顯示:
// SQLDMO.BackupSink_PercentCompleteEventHandler pceh = new SQLDMO.BackupSink_PercentCompleteEventHandler(Step);
// bak.PercentComplete += pceh;
// Step就是上面private void Step(string message,int percent) 的方法名稱,它用來顯示進度條的當前進度。
/// <summary>
/// 資料庫的恢複和殺死進程
/// </summary>
/// <param name="strDbName">資料庫名稱</param>
/// <param name="strFileName">備份檔案名</param>
/// <param name="pgbMain">狀態條控制項名稱</param>
/// <param name="strServerName">伺服器名稱</param>
/// <param name="strUserName">使用者名稱稱</param>
/// <param name="strPwd">密碼</param>
/// <returns>恢複成功返回true ,否則返回false</returns>
public bool RestoreDB(string strDbName,string strFileName, ProgressBar pgbMain,string strServerName,string strUserName,string strPwd )
{
PBar = pgbMain ;
SQLDMO.SQLServer svr = new SQLDMO.SQLServerClass() ;
try
{
ServerName = strServerName ;
UserName = strUserName ;
Password = strPwd ;
svr.Connect(ServerName,UserName,Password) ;
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.Restore res = new SQLDMO.RestoreClass() ;
res.Action = 0 ;
SQLDMO.RestoreSink_PercentCompleteEventHandler pceh = new SQLDMO.RestoreSink_PercentCompleteEventHandler(Step);
res.PercentComplete += pceh;
res.Files = strFileName ;
res.Database = strDbName ;
res.ReplaceDatabase = true ;
res.SQLRestore(svr) ;
return true ;
}
catch(Exception err)
{
// throw(new Exception("恢複資料庫失敗,請關閉所有和該資料庫連接的程式!"+err.Message)) ;
message = "恢複資料庫失敗,請關閉所有和該資料庫連接的程式!" +err.Message;
return false;
}
finally
{
svr.DisConnect() ;
}
}
}
}