[轉貼]ASP.NET下對遠程SQL SERVER資料庫的備份和恢複的預存程序

來源:互聯網
上載者:User

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>
  /// Database Backup
  /// </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 = "Database Backup";
    oBackup.Initialize = true;
    oBackup.SQLBackup(oSQLServer);
   }
   catch
   {
    throw;
   }
  }

  

  /// <summary>
  /// 還原資料庫函數
  /// </summary>
  /// <param name="strDbName">資料庫名</param>
  /// <param name="strFileName">Database Backup檔案的完整路徑名</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() ;
   }
  }

 }
}

相關文章

聯繫我們

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

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

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.