title: How to restore SQL Server database through C# code
body: <p>I try to restore the database like this:</p>
  
  <pre><code>SQL = @"RESTORE DATABASE MyDataBase TO DISK='d:\MyDATA.BAK'";
                  Cmd = new SqlCommand(SQL, Conn);
                  Cmd.ExecuteNonQuery();
                  Cmd.Dispose();
  </code></pre>
  
  <p>but I always get error:</p>
  
  <blockquote>
    <p>Msg 3102, Level 16, State 1, Line 7<br>
    RESTORE cannot process database 'MyDataBase ' because it is in use
    by this session. It is recommended that the master database be used
    when performing this operation.<br>
    Msg 3013, Level 16, State 1, Line 7<br>
    RESTORE DATABASE is terminating abnormally.</p>
  </blockquote>