title: BackUp and Restore Sql Database By Admin User in Asp.Net, C#
body: <p>Here, I have show my c# code for BackUp and Restore with Sql Database</p>
  
  <p>In that back up is working properly but when i try to restore the database from backup folder, that time one error is occurred, and the error is:</p>
  
  <blockquote>
    <p>RESTORE cannot process database &#39;UsersDB&#39; because it is in use
    by this session. It is recommended that the master database be used
    when performing this operation. RESTORE DATABASE is terminating
    abnormally.</p>
  </blockquote>
  
  <p>BackUp button:</p>
  
  <pre><code>protected void btnbackup_Click(object sender, EventArgs e)
      {
          sqlcon.ConnectionString = (System.Configuration.ConfigurationManager.ConnectionStrings["conn"].ToString());
          string destdir = "C:\\backupdb";
          if (!System.IO.Directory.Exists(destdir))
          {
              System.IO.Directory.CreateDirectory("C:\\backupdb");
          }
          try
          {
              sqlcon.Open();
              sqlcmd = new SqlCommand("backup database UsersDB to disk='" + destdir + "\\" + DateTime.Now.ToString("ddMMyyyy_HHmmss") + ".Bak'", sqlcon);
              sqlcmd.ExecuteNonQuery();
              Response.Write("Backup database successfully");
          }
          catch (Exception ex)
          {
              Response.Write("Error During backup database!");
          }
      }
  </code></pre>
  
  <p>Restore Button:</p>
  
  <pre><code> protected void btnrestore_Click(object sender, EventArgs e)
      {
          try
          {
  
  
          sqlcon.ConnectionString = (System.Configuration.ConfigurationManager.ConnectionStrings["conn"].ToString());
          sqlcon.Open();
          string destdir = "C:\\backupdb\\11082014_121403.Bak";
  
          sqlcmd = new SqlCommand("Restore database UsersDB from disk='C:11082014_143650.Bak' ", sqlcon);
          sqlcmd.ExecuteNonQuery();
          Response.Write("restore database successfully");
          }
          catch (Exception ex)
          {
              Response.Write("Error During backup database!");
          }
  
      }
  </code></pre>
  
  <p>Thanks in advance</p>