简体   繁体   English

使用SMO Restore对象还原差异备份

[英]Restoring a Differential Backup with an SMO Restore object

I am trying to restore a database by first restoring a full backup and then restoring a differential backup by using the Microsoft.SqlServer.Management.Smo.Restore class. 我试图通过首先还原完整备份然后使用Microsoft.SqlServer.Management.Smo.Restore类还原差异备份来还原数据库。 The full backup is restored with the following code: 使用以下代码恢复完整备份:

Restore myFullRestore = new Restore();
myFullRestore.Database = "DatabaseName";
myFullRestore.Action = RestoreActionType.Database;
myFullRestore.AddDevice(@"C:\BackupFile.bak", DeviceType.File);
myFullRestore.FileNumber = 1;
myFullRestore.SqlRestore(myServer); // myServer is an already-existing instance of Microsoft.SqlServer.Management.Smo.Server

After restoring the full backup (which completes successfully), my code for restoring the differential backup is as follows: 恢复完整备份(成功完成)后,恢复差异备份的代码如下:

Restore myDiffRestore = new Restore();
myDiffRestore.Database = "DatabaseName";
myDiffRestore.Action = RestoreActionType.Database;
myDiffRestore.AddDevice(@"C:\BackupFile.bak", DeviceType.File);
myDiffRestore.FileNumber = 4; // file contains multiple backup sets, this is the index of the set I want to use
myDiffRestore.SqlRestore(myServer);

However, this code will throw a Microsoft.SqlServer.Management.Smo.FailedOperationException, with the message "Restore failed for server 'servername'". 但是,此代码将抛出Microsoft.SqlServer.Management.Smo.FailedOperationException,并显示消息“为服务器'servername'恢复失败”。 Do I need to explicitly state that I am restoring a differential backup, and if so, how do I go about doing this? 我是否需要明确声明我正在恢复差异备份,如果是,我该如何进行此操作? Or is the problem less obvious than this? 或者这个问题不是很明显吗? Any suggestions as to what I am doing wrong (or neglecting to do) would be greatly appreciated. 任何关于我做错事(或忽视做)的建议都将不胜感激。

Update : Not sure if this was originally a typo or whether earlier versions had this form of the API, but for later versions this line 更新 :不确定这是否是最初的拼写错误或早期版本是否具有此形式的API,但对于此行的更高版本

fullRestore.AddDevice(...);

should be 应该

fullRestore.Devices.AddDevice(...)

After a little bit more digging I figured this one out. 经过一点点的挖掘后,我想出了这个。 In order for the differential backup restore to work, the full restore needs to be performed with NoRecovery set to true: 为了使差异备份还原起作用,需要在NoRecovery设置为true的情况下执行完全还原:

// before executing the SqlRestore command for myFullRestore...
myFullRestore.NoRecovery = true;

This specifies that another transaction log needs to be applied, which in this case is the differential backup. 这指定需要应用另一个事务日志,在这种情况下是差异备份。 This page has some more information that I found useful: http://doc.ddart.net/mssql/sql70/ra-rz_9.htm 这个页面有一些我发现有用的信息: http//doc.ddart.net/mssql/sql70/ra-rz_9.htm

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM