title: SMO : Restore failed for Server '.'
body: <p>I'm using the following SMO code trying to restore a SQL Server database:</p>
  
  <pre><code>Server _server;
  ServerConnection _conn;
  
  public void Restore(string destinationPath)
  {
     Restore res = new Restore();
     _conn = new ServerConnection { ServerInstance = "." };
     _server = new Server(_conn);
  
     try
     {
         string fileName = destinationPath;
         const string databaseName = "RelationAtOffice";
  
         res.Database = databaseName;
         res.Action = RestoreActionType.Database;
         res.Devices.AddDevice(fileName, DeviceType.File);
  
         res.PercentCompleteNotification = 10;
         res.ReplaceDatabase = true;
         res.PercentComplete += new PercentCompleteEventHandler(ProgressEventHandler);
         res.SqlRestore(_server);
  
         System.Windows.Forms.MessageBox.Show("Restore of " + databaseName + " Complete!", "Restore", MessageBoxButtons.OK, MessageBoxIcon.Information);
      }
      catch (SmoException exSMO)
      {
         System.Windows.Forms.MessageBox.Show(exSMO.ToString());
      }
      catch (Exception ex)
      {
          System.Windows.Forms.MessageBox.Show(ex.ToString());
      }
  }
  </code></pre>
  
  <p>Why the code the following link to the correct answer. But my code is not working?
  Code like together. I used the wpf and following link user the winapp</p>
  
  <p><a href="http://www.codeproject.com/Articles/123441/SQL-Server-2008-Backup-and-Restore-Databases-using" rel="nofollow">Link</a></p>
  
  <p>SMO error :</p>
  
  <blockquote>
    <p>Microsoft.SqlServer.Management.Smo.FailedOperationException: Restore
    failed for Server 'MORTEZA'.<br>
    ---> Microsoft.SqlServer.Management.Smo.SmoException:<br>
    System.Data.SqlClient.SqlError: Exclusive access could not be obtained
    because the database is in use.<br>
    at Microsoft.SqlServer.Management.Smo.ExecutionManager.ExecuteNonQueryWithMessage(StringCollection
    queries, ServerMessageEventHandler dbccMessageHandler, Boolean
    errorsAsMessages)<br>
    at Microsoft.SqlServer.Management.Smo.BackupRestoreBase.ExecuteSql(Server
    server, StringCollection queries)<br>
    at Microsoft.SqlServer.Management.Smo.Restore.SqlRestore(Server srv)<br>
    at RelationAtOfficeApp.Admin.AdministratorMainPage.Restore(String
    destinationPath) in
    E:\prozhe\RelationAtOfficeApp\RelationAtOfficeApp\RelationAtOfficeApp\Admin\AdministratorMainPage.xaml.cs:line 268    </p>
  </blockquote>