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. 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'". 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.