title: How to kill all connections to a SQL Server database using C# and SMO?
body: <p>I'm trying to restore a database from a <code>.BAK</code> file using C# and SMO. This is my code.</p>
  
  <pre><code>public static void RestoreDatabase()
  {
      string dbConnString = Configuration.DatabaseConnectionString;
      ServerConnection connection = new ServerConnection(@"dbserver\sqlexpress", "user", "password");
      Server smoServer = new Server(connection);
  
      Restore rstDatabase = new Restore();
      rstDatabase.Action = RestoreActionType.Database;
      rstDatabase.Database = "AppDb";
  
      BackupDeviceItem bkpDevice = new BackupDeviceItem(@"TestData\db-backup.bak", DeviceType.File);
      rstDatabase.Devices.Add(bkpDevice);
      rstDatabase.ReplaceDatabase = true;
  
      // Kill all processes
      smoServer.KillAllProcesses(rstDatabase.Database);
  
      // Set single-user mode
      Database db = smoServer.Databases[rstDatabase.Database];
      db.DatabaseOptions.UserAccess = DatabaseUserAccess.Single;
      db.Alter(TerminationClause.RollbackTransactionsImmediately);
  
      rstDatabase.SqlRestore(smoServer);
  }
  </code></pre>
  
  <p>However when I try to run this method I get the following (error) message when it attempts to kill all processes:</p>
  
  <blockquote>
    <p>Cannot use KILL to kill your own process.</p>
  </blockquote>
  
  <p>I would be very grateful if someone could help solve this issue.</p>