title: Dropping SQL Server database through C#
body: <p>I am using this code to delete a database through C#</p>
  
  <pre><code>Int32 result = 0;
  
  try
  {
          String Connectionstring = CCMMUtility.CreateConnectionString(false, txt_DbDataSource.Text, "master", "sa", "happytimes", 1000);
  
          SqlConnection con = new SqlConnection();
          con.ConnectionString = Connectionstring;
  
          String sqlCommandText = "DROP DATABASE [" + DbName + "]";
          if (con.State == ConnectionState.Closed)
          {
              con.Open();
              SqlConnection.ClearPool(con);
              con.ChangeDatabase("master");
              SqlCommand sqlCommand = new SqlCommand(sqlCommandText, con);
              sqlCommand.ExecuteNonQuery();
          }
          else
          {
              con.ChangeDatabase("master");
              SqlCommand sqlCommand = new SqlCommand(sqlCommandText, con);
              sqlCommand.ExecuteNonQuery();
          }
  
  
  
          con.Close();
          con.Dispose();
          result = 1;
      }
      catch (Exception ex)
      {
          result = 0;
      }
      return result;
  </code></pre>
  
  <p>But I get an error</p>
  
  <blockquote>
    <p><em>Database currently in use</em></p>
  </blockquote>
  
  <p>Can anyone help?</p>