I am trying to restore a database by first restoring a full backup and then restoring a differential backup using the Microsoft.SqlServer.Management.Smo.Restore class. A full backup is restored using 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 a full backup (which succeeds), my code for restoring a 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 Microsoft.SqlServer.Management.Smo.FailedOperationException, with the message "Restore failed for server" server name. Do I need to explicitly indicate that I am restoring a differential backup, and if so, how should I do it? Or a problem less obvious? Any suggestions regarding what I am doing wrong (or neglect to do) would be greatly appreciated.
Refresh . Not sure if this was originally a typo or whether there were previous versions of this form of the API, but for later versions this line
fullRestore.AddDevice(...);
it should be
fullRestore.Devices.AddDevice(...)
Donut source share