2015-08-24 57 views
0

我正在C#中做項目。我在我的項目中使用SQL數據庫。我想投入項目數據庫備份和恢復功能。我必須做什麼/爲它編碼?添加數據庫備份和恢復功能?

+0

http://www.c-sharpcorner.com/UploadFile/rohatash/restore-sql-server-backup-file-with-C-Sharp/ – Nalaka

+0

其中的RDBMS用的?請添加一個標籤來指定您是使用'mysql','postgresql','sql-server','oracle'還是'db2' - 或者其他的東西。 –

+0

我正在使用sql server 2008 –

回答

0

是要備份整個數據庫還是隻想恢復到更改數據庫之前的狀態?

第一種方法在程序中很少見,畢竟:在備份時確定沒有其他人正在更改數據庫嗎?

後者很常見:在對數據庫進行若干更改之前,請記住更改之前的狀態,進行更改並決定是否要在更改之前保留更改或返回到該狀態,例如由於錯誤。

後者是使用System.Data.SqlClient.SqlTransaction類完成的。

using (var sqlConnection = GetSqlConnection(...)) 
{ 
    try (sqlConnection.Open() 
    { // before making the changes: remember the state of the database 
     using (var transaction = sqlConnection.BeginTransaction()) 
     { 
      bool changesOk = true; 
      try 
      { 
       changesOk = ChangeDatabase(...); 
      } 
      catch (Exception exc) 
      { 
       changesOk = false; 
      } 

      if (!changesOk) 
      { // go back to the state at the beginning of the transaction 
       transaction.RollBack(); 
      } 
      else 
      { // no error: save the changes: 
       transaction.Commit(); 
      } 
     } 
    } 
} 
+0

我想備份完整的數據庫並進行恢復。 –

0

**Backup and Restore SQL Server databases programmatically with SMO **

SMO提供了實用工具類爲特定的任務。對於備份和還原,它提供了Microsoft.SqlServer.Management.Smo命名空間中可用的兩個主要實用程序類(備份和還原)。

這是示例代碼。

Backup bkpDBFull = new Backup(); 
    /* Specify whether you want to back up database or files or log */ 
    bkpDBFull.Action = BackupActionType.Database; 
    /* Specify the name of the database to back up */ 
    bkpDBFull.Database = myDatabase.Name; 
    /* You can take backup on several media type (disk or tape), here I am 
    * using File type and storing backup on the file system */ 
    bkpDBFull.Devices.AddDevice(@"D:\AdventureWorksFull.bak", DeviceType.File); 
    bkpDBFull.BackupSetName = "Adventureworks database Backup"; 
    bkpDBFull.BackupSetDescription = "Adventureworks database - Full Backup"; 
    /* You can specify the expiration date for your backup data 
    * after that date backup data would not be relevant */ 
    bkpDBFull.ExpirationDate = DateTime.Today.AddDays(10); 

    /* You can specify Initialize = false (default) to create a new 
    * backup set which will be appended as last backup set on the media. You 
    * can specify Initialize = true to make the backup as first set on the 
    * medium and to overwrite any other existing backup sets if the all the 
    * backup sets have expired and specified backup set name matches with 
    * the name on the medium */ 
    bkpDBFull.Initialize = false; 

    /* Wiring up events for progress monitoring */ 
    bkpDBFull.PercentComplete += CompletionStatusInPercent; 
    bkpDBFull.Complete += Backup_Completed; 

    /* SqlBackup method starts to take back up 
    * You can also use SqlBackupAsync method to perform the backup 
    * operation asynchronously */ 
    bkpDBFull.SqlBackup(myServer); 
    private static void CompletionStatusInPercent(object sender, PercentCompleteEventArgs args) 
    { 
     Console.Clear(); 
     Console.WriteLine("Percent completed: {0}%.", args.Percent); 
    } 
    private static void Backup_Completed(object sender, ServerMessageEventArgs args) 
    { 
     Console.WriteLine("Hurray...Backup completed."); 
     Console.WriteLine(args.Error.Message); 
    } 
    private static void Restore_Completed(object sender, ServerMessageEventArgs args) 
    { 
     Console.WriteLine("Hurray...Restore completed."); 
     Console.WriteLine(args.Error.Message); 
    }