SMO:服务器'还原失败.'

MMD*_*MNC 3 sql-server wpf smo restore c#-4.0

我正在使用以下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());
    }
}
Run Code Online (Sandbox Code Playgroud)

为什么以下代码链接到正确的答案.但我的代码不起作用?代码就像在一起.我使用wpf和以下链接用户winapp

链接

SMO错误:

Microsoft.SqlServer.Management.Smo.FailedOperationException:服务器'MORTEZA'的还原失败.
---> Microsoft.SqlServer.Management.Smo.SmoException:
System.Data.SqlClient.SqlError:由于数据库正在使用,无法获取独占访问权限.
在Microsoft.SqlServer.Management.Smo的
Microsoft.SqlServer.Management.Smo.BackupRestoreBase.ExecuteSql(服务器服务器,StringCollection查询)
中的Microsoft.SqlServer.Management.Smo.ExecutionManager.ExecuteNonQueryWithMessage (StringCollection queries,ServerMessageEventHandler dbccMessageHandler,Boolean errorsAsMessages).
位于E:\ prozhe\RelationAtOfficeApp\RelationAtOfficeApp\RelationAtOfficeApp\Admin\AdministratorMainPage.xaml.cs中的RelationAtOfficeApp.Admin.AdministratorMainPage.Restore(String destinationPath)的Restore.SqlRestore(Server srv):第268行

mar*_*c_s 7

最可能的原因是:

  • 你已经采取了从备份服务器-备份Data.mdfData_Log.ldf成一个Backup.bak文件

  • 在同一台(服务器)计算机上,您现在正在尝试还原同一个数据库

在这种情况下,.mdf并且.ldf应该被改写-但是这不会发生,因为SQL Server仍然有它的控制下,该数据库-这样的还原会失败,因为数据和日志文件不能被覆盖.

有两种方法可以解决这个问题:

  1. 定义"文件重定位",例如在恢复时定义新数据和日志文件名.这样,您的数据库现在就会恢复,并且文件将以新名称放在SQL Server数据目录中.

    这需要像他的代码:

    ....  
    res.Devices.AddDevice(fileName, DeviceType.File);
    
    // define "file relocation" - for all "logical" files in a SQL Server database,
    // define a new physical location where the file will end up at          
    RelocateFile relocateDataFile = new RelocateFile("Data", @"(your data dir)\RestoredData.mdf");
    RelocateFile relocateLogFile = new RelocateFile("Log", @"(your log dir)\Data\RestoredData_log.ldf");
    
    res.RelocateFiles.Add(relocateDataFile);
    res.RelocateFiles.Add(relocateLogFile);
    
    Run Code Online (Sandbox Code Playgroud)
  2. 如果您不想创建新文件,则应该能够通过设置以下内容来指定要使用还原操作替换现有数据库的SMO Restore:

    res.ReplaceDatabase = true;
    
    Run Code Online (Sandbox Code Playgroud)

    在打电话之前

    res.SqlRestore(_server);
    
    Run Code Online (Sandbox Code Playgroud)

更新:我在示例中使用的逻辑文件名当然只是示例 - 在您的情况下,您的备份文件很可能包含其他逻辑文件名,您需要在代码中使用这些文件名.

您可以通过两种方式找出数据库包含的逻辑文件名:

  1. 如明确说明错误消息 - 您可以在SQL Server Management Studio中使用该命令FILELISTONLY上的选项RESTORE,从而.bak在还原之前检查该文件.尝试这样的事情:

    RESTORE FILELISTONLY
    FROM DISK = N'path-and-full-file-name-of-your-bak-file-here.bak'
    
    Run Code Online (Sandbox Code Playgroud)

    这将为您提供一个包含文件中包含的逻辑和物理文件名的小网格.bak.

  2. 如果数据库仍附加到服务器,则可以使用Object ExplorerSQL Server Management Studio中的数据库来查找数据库的逻辑文件名.右键单击您选择的数据库,然后您将看到此对话框,在该Files部分中,您将获得您要查找的信息:

在此输入图像描述