title: Restoring a Differential Backup with an SMO Restore object
body: <p>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:</p>
  
  <pre><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
  </code></pre>
  
  <p>After restoring the full backup (which completes successfully), my code for restoring the differential backup is as follows:</p>
  
  <pre><code>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);
  </code></pre>
  
  <p>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.</p>
  
  <p><strong>Update</strong>: Not sure if this was originally a typo or whether earlier versions had this form of the API, but for later versions this line</p>
  
  <pre><code>fullRestore.AddDevice(...);
  </code></pre>
  
  <p>should be</p>
  
  <pre><code>fullRestore.Devices.AddDevice(...)
  </code></pre>