Microsoft SQL ServerDatabase Backup和恢複的類)

來源:互聯網
上載者:User

C#+SqlDatabase Backup
1.在項目中添加SQLDmo dll檔案引用(SQLDMO(SQL Distributed Management Objects,SQL分散式管理物件))
2在相應頁面加using SQLDMO引用
3.下面是用C#語言書寫的用於Microsoft SQL ServerDatabase Backup和恢複的類:

using System;

namespace DbService
{
/// <summary>
/// DbOper類,主要實現對Microsoft SQL Server資料庫的備份和恢複
/// </summary>
public sealed class DbOper
{
/// <summary>
/// DbOper類的建構函式
/// </summary>
private DbOper()
{
}

/// <summary>
/// Database Backup
/// </summary>
public static void DbBackup()
{
try
{
SQLDMO.Backup oBackup = new SQLDMO.BackupClass();
SQLDMO.SQLServer oSQLServer = new SQLDMO.SQLServerClass();
oSQLServer.LoginSecure = false;
oSQLServer.Connect("localhost", "sa", "1234");
oBackup.Action = SQLDMO.SQLDMO_BACKUP_TYPE.SQLDMOBackup_Database;
oBackup.Database = "Northwind";
oBackup.Files = @"d:\\Northwind.bak";
oBackup.BackupSetName = "Northwind";
oBackup.BackupSetDescription = "Database Backup";
oBackup.Initialize = true;
oBackup.SQLBackup(oSQLServer);
}
catch
{
throw;
}
}

/// <summary>
/// 資料庫恢複
/// </summary>
public static void DbRestore()
{
try
{
SQLDMO.Restore oRestore = new SQLDMO.RestoreClass();
SQLDMO.SQLServer oSQLServer = new SQLDMO.SQLServerClass();
oSQLServer.LoginSecure = false;
oSQLServer.Connect("localhost", "sa", "1234");
oRestore.Action = SQLDMO.SQLDMO_RESTORE_TYPE.SQLDMORestore_Database;
oRestore.Database = "Northwind";
oRestore.Files = @"d:\\Northwind.bak";
oRestore.FileNumber = 1;
oRestore.ReplaceDatabase = true;
oRestore.SQLRestore(oSQLServer);
}
catch
{
throw;
}
}
}
}
參見在C#中運用SQLDMO備份和恢複Microsoft SQL Server資料庫

http://dev.csdn.net/develop/article/28/28564.shtm

當不使用要恢複的資料庫時以上方法可行,但當你使用了資料庫時就必須殺死該進程
代碼如下:

  /// <summary>
  /// 還原資料庫函數
  /// </summary>
  /// <param name="strDbName">資料庫名</param>
  /// <param name="strFileName">Database Backup檔案的完整路徑名</param>
  /// <returns></returns>
  public bool RestoreDB(string strDbName,string strFileName)
  {
   //PBar = pgbMain ;
   SQLDMO.SQLServer svr = new SQLDMO.SQLServerClass() ;
   try
   {
    //伺服器名,資料庫使用者名稱,資料庫使用者名稱密碼
    svr.Connect("localhost","sa","hai") ;
   
    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() ;
   }
  }
你就可以通過使用者佈建的時間來備份資料庫了,可以用定時器實現
或是調用預存程序:
方法為:
還原整個資料庫:

RESTORE DATABASE { database_name | @database_name_var }
[ FROM < backup_device > [ ,...n ] ]
[ WITH
    [ RESTRICTED_USER ]
    [ [ , ] FILE = { file_number | @file_number } ]
    [ [ , ] PASSWORD = { password | @password_variable } ]
    [ [ , ] MEDIANAME = { media_name | @media_name_variable } ]
    [ [ , ] MEDIAPASSWORD = { mediapassword | @mediapassword_variable } ]
    [ [ , ] MOVE 'logical_file_name' TO 'operating_system_file_name' ]
            [ ,...n ]
    [ [ , ] KEEP_REPLICATION ]
    [ [ , ] { NORECOVERY | RECOVERY | STANDBY = undo_file_name } ]
    [ [ , ] { NOREWIND | REWIND } ]
    [ [ , ] { NOUNLOAD | UNLOAD } ]
    [ [ , ] REPLACE ]
    [ [ , ] RESTART ]
    [ [ , ] STATS [ = percentage ] ]
]

還原資料庫的部分內容:

RESTORE DATABASE { database_name | @database_name_var }
    < file_or_filegroup > [ ,...n ]
[ FROM < backup_device > [ ,...n ] ]
[ WITH
    { PARTIAL }
    [ [ , ] FILE = { file_number | @file_number } ]
    [ [ , ] PASSWORD = { password | @password_variable } ]
    [ [ , ] MEDIANAME = { media_name | @media_name_variable } ]
    [ [ , ] MEDIAPASSWORD = { mediapassword | @mediapassword_variable } ]
    [ [ , ] MOVE 'logical_file_name' TO 'operating_system_file_name' ]
            [ ,...n ]
    [ [ , ] NORECOVERY ]
    [ [ , ] { NOREWIND | REWIND } ]
    [ [ , ] { NOUNLOAD | UNLOAD } ]
    [ [ , ] REPLACE ]
    [ [ , ] RESTRICTED_USER ]
    [ [ , ] RESTART ]
    [ [ , ] STATS [ = percentage ] ]
]

還原特定的檔案或檔案組:

RESTORE DATABASE { database_name | @database_name_var }
    < file_or_filegroup > [ ,...n ]
[ FROM < backup_device > [ ,...n ] ]
[ WITH
    [ RESTRICTED_USER ]
    [ [ , ] FILE = { file_number | @file_number } ]
    [ [ , ] PASSWORD = { password | @password_variable } ]
    [ [ , ] MEDIANAME = { media_name | @media_name_variable } ]
    [ [ , ] MEDIAPASSWORD = { mediapassword | @mediapassword_variable } ]
    [ [ , ] MOVE 'logical_file_name' TO 'operating_system_file_name' ]
            [ ,...n ]
    [ [ , ] NORECOVERY ]
    [ [ , ] { NOREWIND | REWIND } ]
    [ [ , ] { NOUNLOAD | UNLOAD } ]
    [ [ , ] REPLACE ]
    [ [ , ] RESTART ]
    [ [ , ] STATS [ = percentage ] ]
]

還原交易記錄:

RESTORE LOG { database_name | @database_name_var }
[ FROM < backup_device > [ ,...n ] ]
[ WITH
    [ RESTRICTED_USER ]
    [ [ , ] FILE = { file_number | @file_number } ]
    [ [ , ] PASSWORD = { password | @password_variable } ]
    [ [ , ] MOVE 'logical_file_name' TO 'operating_system_file_name' ]
            [ ,...n ]
    [ [ , ] MEDIANAME = { media_name | @media_name_variable } ]
    [ [ , ] MEDIAPASSWORD = { mediapassword | @mediapassword_variable } ]
    [ [ , ] KEEP_REPLICATION ]
    [ [ , ] { NORECOVERY | RECOVERY | STANDBY = undo_file_name } ]
    [ [ , ] { NOREWIND | REWIND } ]
    [ [ , ] { NOUNLOAD | UNLOAD } ]
    [ [ , ] RESTART ]
    [ [ , ] STATS [= percentage ] ]
    [ [ , ] STOPAT = { date_time | @date_time_var }
        | [ , ] STOPATMARK = 'mark_name' [ AFTER datetime ]
        | [ , ] STOPBEFOREMARK = 'mark_name' [ AFTER datetime ]
    ]
]

< backup_device > ::=
    {
        { 'logical_backup_device_name' | @logical_backup_device_name_var }
        | { DISK | TAPE } =
            { 'physical_backup_device_name' | @physical_backup_device_name_var }
    }

< file_or_filegroup > ::=
    {
        FILE = { logical_file_name | @logical_file_name_var }
        |
        FILEGROUP = { logical_filegroup_name | @logical_filegroup_name_var}
    }

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.