次の SMO コードを使用して、SQL Server データベースを復元しようとしています。
Server _server;
ServerConnection _conn;
public void Restore(string destinationPath)
{
Restore res = new Restore();
_conn = new ServerConnection { ServerInstance = "." };
_server = new Server(_conn);
try
{
string fileName = destinationPath;
const string databaseName = "RelationAtOffice";
res.Database = databaseName;
res.Action = RestoreActionType.Database;
res.Devices.AddDevice(fileName, DeviceType.File);
res.PercentCompleteNotification = 10;
res.ReplaceDatabase = true;
res.PercentComplete += new PercentCompleteEventHandler(ProgressEventHandler);
res.SqlRestore(_server);
System.Windows.Forms.MessageBox.Show("Restore of " + databaseName + " Complete!", "Restore", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (SmoException exSMO)
{
System.Windows.Forms.MessageBox.Show(exSMO.ToString());
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.ToString());
}
}
次のコードを正しい答えにリンクする理由。しかし、私のコードは機能していませんか? 一緒に好きなコード。私はwpfを使用し、次のリンクユーザーはwinappを使用しました
SMO エラー:
Microsoft.SqlServer.Management.Smo.FailedOperationException: サーバー 'MORTEZA' の復元に失敗しました。
---> Microsoft.SqlServer.Management.Smo.SmoException:
System.Data.SqlClient.SqlError: データベースが使用中のため、排他アクセスを取得できませんでした。
Microsoft.SqlServer.Management.Smo.ExecutionManager.ExecuteNonQueryWithMessage (StringCollection クエリ、ServerMessageEventHandler dbccMessageHandler、Boolean errorsAsMessages)
で Microsoft.SqlServer.Management.Smo.BackupRestoreBase.ExecuteSql (サーバー サーバー、StringCollection クエリ)
で Microsoft.SqlServer.Management.Smo. Restore.SqlRestore(サーバーサーバー)
E:\prozhe\RelationAtOfficeApp\RelationAtOfficeApp\RelationAtOfficeApp\Admin\AdministratorMainPage.xaml.cs:line 268 の RelationAtOfficeApp.Admin.AdministratorMainPage.Restore (String destinationPath) で