2013-01-17 27 views
12

SQL Server SMOを使用して、.bakを新しいデータベースに復元しましたが、機能しませんでした。smoリストアデータベース

のSQL Server 2012とSMOオブジェクトバージョンが11.0

ファイル.BAKも同様に同じ符号化のPC上、SQL管理スタジオ2012、同じローカルPCを使用して作成された最新のSDKのバージョンからです。

私が取得エラーメッセージは次のとおりです。

復元がサーバ「サーバ」に失敗しました。

私のコードに間違いがありますか?

string dbPath = Path.Combine(@"d:\my data", dbName + "_db" + ".mdf"); 
string logPath = Path.Combine(@"d:\my data", dbName + "_db" + "_Log.ldf"); 

Restore restore = new Restore(); 

BackupDeviceItem deviceItem = new BackupDeviceItem("d:\template.BAK", DeviceType.File); 
restore.Devices.Add(deviceItem); 
restore.Database = dbName + "_db"; 

RelocateFile relocateDataFile = new RelocateFile("Data", dbPath); 
RelocateFile relocateLogFile = new RelocateFile("Log", logPath); 

restore.RelocateFiles.Add(relocateDataFile); 
restore.RelocateFiles.Add(relocateLogFile); 

restore.Action = RestoreActionType.Database; 
restore.ReplaceDatabase = true; 
restore.SqlRestore(server); 

更新日:私は、SMOソリューションをsurrended、および

using (SqlConnection connection = new SqlConnection("Data Source=server;user id=sa;password=xxxxx;")) 
     { 

      using (SqlCommand command = new SqlCommand(@"RESTORE DATABASE beauty01 FROM DISK = 'd:\template.bak' WITH RECOVERY, MOVE 'beauty1' TO 'D:\MyData\beauty01_Data.mdf', MOVE 'beauty1_log' TO 'd:\Mydata\beauty01_Log.ldf', REPLACE", connection)) 
      { 
       connection.Open(); 
       // Add the parameters for the SelectCommand. 


       command.CommandType = CommandType.Text; 
       command.ExecuteNonQuery(); 
      } 

     } >> work good. 

おかげですべてを試してみました。

+0

内部例外はありますか?おそらくあなたに本当の理由を与えるでしょうデバッグをチェックしてください。 – Bridge

+0

また、既に存在するファイルを上書きしようとしていませんか?同じ 'dbName'を使用する場合、同じ名前のデータとログファイルを持つことができます。ファイルが最初に存在するかどうかを確認し、存在する場合は再度作成しないでください。 – Bridge

+0

バックアップデバイス 'd:\ template.BAK'を開くことができません。オペレーティングシステムエラー123(ファイル名、ディレクトリ名、またはボリュームラベルの構文が間違っています)。 >> .bakはSQL Management Studio 2012によって作成され、smoは正しいバージョン(バージョン11)です。 –

答えて

21

SMOを使用してデータベースを正常に復元しました。私は自分のコードを共有します。それが役に立てば幸い。しかし、この解決策には1つの注意点があります。プライマリデータファイルが1つしかないと考えられます。ログファイルとデータファイルのマッチングは非常に難しく、何かが間違っている可能性があります。とにかくこれが助けになることを私に知らせてください。

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.IO; 
using System.Text; 
using System.Threading; 
using Microsoft.SqlServer.Management.Common; 
using Microsoft.SqlServer.Management.Smo; 
using Microsoft.Win32; 

namespace DatabaseUtility 
{ 
    public class BackupRestore 
    { 
     static Server srv; 
     static ServerConnection conn; 

     public static void BackupDatabase(string serverName, string databaseName, string filePath) 
     { 
      conn = new ServerConnection(); 
      conn.ServerInstance = serverName; 
      srv = new Server(conn); 

      try 
      { 
       Backup bkp = new Backup(); 

       bkp.Action = BackupActionType.Database; 
       bkp.Database = databaseName; 

       bkp.Devices.AddDevice(filePath, DeviceType.File); 
       bkp.Incremental = false; 

       bkp.SqlBackup(srv); 

       conn.Disconnect(); 
       conn = null; 
       srv = null; 
      } 

      catch (SmoException ex) 
      { 
       throw new SmoException(ex.Message, ex.InnerException); 
      } 
      catch (IOException ex) 
      { 
       throw new IOException(ex.Message, ex.InnerException); 
      } 
     } 

     public static void RestoreDatabase(string serverName, string databaseName, string filePath) 
     { 

      conn = new ServerConnection(); 
      conn.ServerInstance = serverName; 
      srv = new Server(conn); 

      try 
      { 
       Restore res = new Restore(); 

       res.Devices.AddDevice(filePath, DeviceType.File); 

       RelocateFile DataFile = new RelocateFile(); 
       string MDF = res.ReadFileList(srv).Rows[0][1].ToString(); 
       DataFile.LogicalFileName = res.ReadFileList(srv).Rows[0][0].ToString(); 
       DataFile.PhysicalFileName = srv.Databases[databaseName].FileGroups[0].Files[0].FileName; 

       RelocateFile LogFile = new RelocateFile(); 
       string LDF = res.ReadFileList(srv).Rows[1][1].ToString(); 
       LogFile.LogicalFileName = res.ReadFileList(srv).Rows[1][0].ToString(); 
       LogFile.PhysicalFileName = srv.Databases[databaseName].LogFiles[0].FileName; 

       res.RelocateFiles.Add(DataFile); 
       res.RelocateFiles.Add(LogFile); 

       res.Database = databaseName; 
       res.NoRecovery = false; 
       res.ReplaceDatabase = true; 
       res.SqlRestore(srv); 
       conn.Disconnect(); 
      } 
      catch (SmoException ex) 
      { 
       throw new SmoException(ex.Message, ex.InnerException); 
      } 
      catch (IOException ex) 
      { 
       throw new IOException(ex.Message, ex.InnerException); 
      } 
     } 

     public static Server Getdatabases(string serverName) 
     { 
      conn = new ServerConnection(); 
      conn.ServerInstance = serverName; 

      srv = new Server(conn); 
      conn.Disconnect(); 
      return srv; 

     } 
    } 
} 
+0

このコードはいくつかの変更を加えて本当に助けになりました。私は別の場所にDBを復元する必要があったし、RelocateFilesというものはそのままではうまくいきました。 – Jez

+0

こんにちは、データベースの復元操作でファイルを再配置する目的は何ですか? –

+0

@RustyWizardデータベースファイルがデフォルトの場所に存在しないことがあります。その場合、ファイルを見つけてrestoreコマンドで使用して、リストア中にエラーがないようにする必要があります。 –

関連する問題