2010-10-05 53 views
4
public void RestoreDatabase(String databaseName, String filePath, 
      String serverName, String userName, String password, String dataFilePath, String logFilePath) 
{ 
    Restore sqlRestore = new Restore(); 
    BackupDeviceItem deviceItem = new BackupDeviceItem(filePath, DeviceType.File); 
    sqlRestore.Devices.Add(deviceItem); 
    sqlRestore.Database = databaseName; 
    ServerConnection connection = new ServerConnection(serverName, userName, password); 
    Server sqlServer = new Server(connection); 
    Database db = sqlServer.Databases[databaseName]; 
    sqlRestore.Action = RestoreActionType.Database; 
    String dataFileLocation = dataFilePath; 
    String logFileLocation = logFilePath; 
    db = sqlServer.Databases[databaseName]; 
    sqlRestore.RelocateFiles.Add(new RelocateFile(databaseName, dataFileLocation)); 
    sqlRestore.RelocateFiles.Add(new RelocateFile(databaseName + "_log", logFileLocation)); 
    sqlRestore.ReplaceDatabase = true; 
    sqlRestore.Complete +=new ServerMessageEventHandler(sqlRestore_Complete); 
    sqlRestore.SqlRestore(sqlServer); 
    db = sqlServer.Databases[databaseName]; 
    db.SetOnline(); 
    sqlServer.Refresh(); 
} 

的恢復在調用此方法還原操作失敗,此消息如何使用恢復類Microsoft.SqlServer.Management.Smo命名空間

Restore failed for Server 'MDM04\SQLEXPRESS'.

mdm04是我的計算機名

內例外是

"System.Data.SqlClient.SqlError: Logical file 'vrv' is not part of database 'vrv'. Use RESTORE FILELISTONLY to list the logical file names."

vrv是數據庫名稱

我應該怎麼做來恢復文件

回答

15

問題是在這裏

sqlRestore.RelocateFiles.Add(new RelocateFile(databaseName, dataFileLocation)); 
sqlRestore.RelocateFiles.Add(new RelocateFile(databaseName + "_log", logFileLocation)); 

這裏的databaseName手段,以dB爲單位的備份文件中指定的數據庫的名稱。但是你正在指定目標數據庫名稱。

將其更改爲原來的數據庫名

這裏的示例代碼來讀取備份文件DB名

DataTable dtFileList = sqlRestore.ReadFileList(serverName); 
string dbLogicalName = dtFileList.Rows[0][0].ToString(); 
string dbPhysicalName = dtFileList.Rows[0][1].ToString(); 
string logLogicalName = dtFileList.Rows[1][0].ToString(); 
string logPhysicalName = dtFileList.Rows[1][1].ToString 
+0

你真棒謝謝你。我整天都在拼命搜索,這是第一次有人指出數據庫名稱的真正含義。 - 所以後續問題:如何在還原發生後更改邏輯名稱? – Colema18 2015-07-24 18:10:26

0

現在轉到「瀏覽」選項卡,然後瀏覽以下路徑 -

Ç :\ Program Files \ Microsoft SQL Server \ 100 \ SDK \ Assemblies 或 C:\ Program Files \ Microsoft SQL Server \ 110 \ SDK \ Assemblies

現在選擇下列DLL

Microsoft.SqlServer.ConnectionInfo.dll

Microsoft.SqlServer.Management.Sdk.Sfc.dll

Microsoft.SqlServer.Smo.dll

Microsoft.SqlServer .SmoExtended.dll

Microsoft.SqlServer.SqlEnum.dll

  public void RestoreDatabase(String databaseName, String filePath, 
     String serverName, String userName, String password, 
     String dataFilePath, String logFilePath) 
     { 
      Restore sqlRestore = new Restore(); 
      BackupDeviceItem deviceItem = new BackupDeviceItem(filePath, DeviceType.File); 
      sqlRestore.Devices.Add(deviceItem); 
      sqlRestore.Database = databaseName; 
      ServerConnection connection = new ServerConnection(serverName, userName, password); 
      Server sqlServer = new Server(connection); 
      Database db = sqlServer.Databases[databaseName]; 
      sqlRestore.Action = RestoreActionType.Database; 
      String dataFileLocation = dataFilePath; 
      String logFileLocation = logFilePath; 
      db = sqlServer.Databases[databaseName]; 
      sqlRestore.RelocateFiles.Add(new RelocateFile(databaseName, dataFileLocation)); 
      sqlRestore.RelocateFiles.Add(new RelocateFile(databaseName + "_log", logFileLocation)); 
      sqlRestore.ReplaceDatabase = true; 
      sqlRestore.Complete +=new ServerMessageEventHandler(sqlRestore_Complete); 
      sqlRestore.SqlRestore(sqlServer); 
      db = sqlServer.Databases[databaseName]; 
      db.SetOnline(); 
      sqlServer.Refresh(); 
     }