程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> sqlserver備份復原數據庫功效封裝分享

sqlserver備份復原數據庫功效封裝分享

編輯:C#入門知識

sqlserver備份復原數據庫功效封裝分享。本站提示廣大學習愛好者:(sqlserver備份復原數據庫功效封裝分享)文章只能為提供參考,不一定能成為您想要的結果。以下是sqlserver備份復原數據庫功效封裝分享正文




using System;
using SQLDMO;

namespace EDTBackupTool
{
 /// <summary>
 /// Backup 的摘要解釋。
 /// </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 = "數據庫備份";
    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 ;
    }
    //殺逝世應用strDbName數據庫的過程
    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.WordStrDatabase = true ;
    res.SQLRestore(svr) ;
    return true ;
   }
   catch
   {
    return false;
   }
   finally
   {
    svr.DisConnect() ;
   }
  }
 }
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved