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.
おかげですべてを試してみました。
内部例外はありますか?おそらくあなたに本当の理由を与えるでしょうデバッグをチェックしてください。 – Bridge
また、既に存在するファイルを上書きしようとしていませんか?同じ 'dbName'を使用する場合、同じ名前のデータとログファイルを持つことができます。ファイルが最初に存在するかどうかを確認し、存在する場合は再度作成しないでください。 – Bridge
バックアップデバイス 'd:\ template.BAK'を開くことができません。オペレーティングシステムエラー123(ファイル名、ディレクトリ名、またはボリュームラベルの構文が間違っています)。 >> .bakはSQL Management Studio 2012によって作成され、smoは正しいバージョン(バージョン11)です。 –