title: Database restore error in C#
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/4046708/exclusive-access-could-not-be-obtained-because-the-database-is-in-use">Exclusive access could not be obtained because the database is in use</a>  </p>
  </blockquote>
  
  
  
  <p>I use C#, .net4 and SQL Server 2008 R2 in project and use this code to restore a database:</p>
  
  <pre><code>_comm = new SqlCommand("use master; RESTORE DATABASE [DB1] FROM  DISK = @Address WITH  RESTRICTED_USER,  FILE = 1,  NOUNLOAD,  REPLACE,  STATS = 10; use DB1;", _conn);
          _comm.CommandType = System.Data.CommandType.Text;
          _comm.Parameters.AddRange(new SqlParameter[] 
                  {
                  new SqlParameter("@Address", _path)
                  });
  
              _conn.Open();
              _comm.ExecuteNonQuery();
  </code></pre>
  
  <p>The following error is displayed:</p>
  
  <blockquote>
    <p>Exclusive access could not be obtained because the database is in use.
     RESTORE DATABASE is terminating abnormally.
     Changed database context to 'master'.
     Changed database context to 'DB1'.</p>
  </blockquote>