ASP.NET下對遠程SQL SERVER數據庫的備份和恢復

 

Sql server的幫助文檔地址,在IE裏面直接輸入就行
mk:@MSITStore:C:\Program%20Files\Microsoft%20SQL%20Server\80\Tools\Books\tsqlref.chm::/ts_ba-bz_35ww.htm

backup database to disk path備份
restore database from disk path恢復


using System;
using System.Collections;
using System.Security.Cryptography;

using System.Data;
using System.Data.SqlClient;
using System.Web;

using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using System.Diagnostics ;
using System.Text ;
using System.ComponentModel;
using System.Configuration;
using System.Data.OleDb;

namespace DbService
{
 /// <summary>
 /// DbOper類,主要實現對Microsoft SQL Server數據庫的備份和恢復
 /// </summary>
 public sealed class DbOper
 {
  /// <summary>
  /// DbOper類的構造函數
  /// </summary>
  //private DbOper()
  //{
  //}

  /// <summary>
  /// 數據庫備份
  /// </summary>
  /// 
  public static string BackFileName=System.Web.HttpContext.Current.Request.PhysicalApplicationPath+"BackUp\\MyDbBack.bak";
  public static void DbBackup(string DbNanme,string userid,string pwd)
  {
   try
   {
    SQLDMO.Backup oBackup = new SQLDMO.BackupClass();
    SQLDMO.SQLServer oSQLServer = new SQLDMO.SQLServerClass();
    oSQLServer.LoginSecure = false;
    oSQLServer.Connect("localhost",userid,pwd);
    oBackup.Action = SQLDMO.SQLDMO_BACKUP_TYPE.SQLDMOBackup_Database;
    oBackup.Database = DbNanme;
    oBackup.Files =BackFileName;
    oBackup.BackupSetName =DbNanme;
    oBackup.BackupSetDescription = "數據庫備份";
    oBackup.Initialize = true;
    oBackup.SQLBackup(oSQLServer);
   }
   catch
   {
    throw;
   }
  }

  

  /// <summary>
  /// 還原數據庫函數
  /// </summary>
  /// <param name="strDbName">數據庫名</param>
  /// <param name="strFileName">數據庫備份文件的完整路徑名</param>
  /// <returns></returns>
  public bool RestoreDB(string strDbName,string strFileName,string userid,string pwd)
  { 
   //PBar = pgbMain ; 
   SQLDMO.SQLServer svr = new SQLDMO.SQLServerClass() ; 
   try 
   { 
    //服務器名,數據庫用戶名,數據庫用戶名密碼
    svr.Connect("localhost",userid,pwd) ;
    
    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 ; 
    } 
    //殺死使用strDbName數據庫的進程
    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 ; 
    res.Files = strFileName ; 

    res.Database = strDbName ; 
    res.ReplaceDatabase = true ; 
    res.SQLRestore(svr) ; 
    return true ; 
   } 
   catch
   { 
    return false;
   } 
   finally 
   { 
    svr.DisConnect() ; 
   } 
  } 

 }
}


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章