SQL Server SMO を使用して .bak を新しいデータベースに復元しましたが、うまくいきませんでした。
SQLサーバーは2012年で、smoオブジェクトのバージョンは最新のSDKバージョン11.0のものです
ファイル .bak は、同じコーディング pc で、同じローカル pc の sql management studio 2012 を使用して作成されました。
私が得るエラーメッセージは次のとおりです。
サーバー 'SERVER' の復元に失敗しました。
コードの何が問題になっていますか?
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 ソリューションを放棄し、試してみました
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.
皆さんありがとう。