我正在使用以下 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.BackupRestoreBase.ExecuteSql (服务器服务器,StringCollection 查询)在 Microsoft.SqlServer.Management.Smo
的 Microsoft.SqlServer.Management.Smo.ExecutionManager.ExecuteNonQueryWithMessage(StringCollection 查询,ServerMessageEventHandler dbccMessageHandler,布尔错误AsMessages) 。 Restore.SqlRestore(服务器 srv)
在 RelationAtOfficeApp.Admin.AdministratorMainPage.Restore(String destinationPath) 在 E:\prozhe\RelationAtOfficeApp\RelationAtOfficeApp\RelationAtOfficeApp\Admin\AdministratorMainPage.xaml.cs:line 268