我正在尝试通过首先还原完整备份然后使用 Microsoft.SqlServer.Management.Smo.Restore 类还原差异备份来还原数据库。使用以下代码恢复完整备份:
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
恢复完整备份(成功完成)后,我恢复差异备份的代码如下:
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);
但是,此代码将引发 Microsoft.SqlServer.Management.Smo.FailedOperationException,并显示消息“Restore failed for server 'servername'”。我是否需要明确声明我正在恢复差异备份,如果需要,我该怎么做?还是问题比这不那么明显?任何关于我做错了什么(或忽略做)的建议将不胜感激。
更新:不确定这是否最初是一个错字或早期版本是否有这种形式的 API,但对于后来的版本,这条线
fullRestore.AddDevice(...);
应该
fullRestore.Devices.AddDevice(...)