Why are there connections open to my databases?

Posted by Everett on Stack Overflow See other posts from Stack Overflow or by Everett
Published on 2010-06-06T02:01:58Z Indexed on 2010/06/06 2:12 UTC
Read the original article Hit count: 457

I have a program that stores user projects as databases. Naturally, the program should allow the user to create and delete the databases as they need to. When the program boots up, it looks for all the databases in a specific SQLServer instance that have the structure the program is expecting. These database are then loaded into a listbox so the user can pick one to open as a project to work on.

When I try to delete a database from the program, I always get an SQL error saying that the database is currently open and the operation fails. I've determined that the code that checks for the databases to load is causing the problem. I'm not sure why though, because I'm quite sure that all the connections are being properly closed.

Here are all the relevant functions. After calling BuildProjectList, running "DROP DATABASE database_name" from ExecuteSQL fails with the message: "Cannot drop database because it is currently in use". I'm using SQLServer 2005.

private SqlConnection databaseConnection;
private string connectionString;
private ArrayList databases;

public ArrayList BuildProjectList()
{
    //databases is an ArrayList of all the databases in an instance
    if (databases.Count <= 0)
    {
      return null;
    }
    ArrayList databaseNames = new ArrayList();
    for (int i = 0; i < databases.Count; i++)
    {
      string db = databases[i].ToString();
      connectionString = "Server=localhost\\SQLExpress;Trusted_Connection=True;Database=" + db + ";";
      //Check if the database has the table required for the project
      string sql = "select * from TableExpectedToExist";
      if (ExecuteSQL(sql)) {
        databaseNames.Add(db);
      }
    }
    return databaseNames;
}

private bool ExecuteSQL(string sql)
{
    bool success = false;
    openConnection();
    SqlCommand cmd = new SqlCommand(sql, databaseConnection);
    try
    {
      cmd.ExecuteNonQuery();
      success = true;
    }
    catch (SqlException ae)
    {
      MessageBox.Show(ae.Message.ToString());
    }
    closeConnection();
    return success;
}

public void openConnection()
{
    databaseConnection = new SqlConnection(connectionString);
    try
    {
      databaseConnection.Open();
    }
    catch(Exception e)
    {
      MessageBox.Show(e.ToString(), "Error", 
      MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
}

public void closeConnection()
{
    if (databaseConnection != null)
    {
      try
      {
        databaseConnection.Close();
      }
      catch (Exception e)
      {
        MessageBox.Show(e.ToString(), "Error",
        MessageBoxButtons.OK, MessageBoxIcon.Error);
      }
    }
}

© Stack Overflow or respective owner

Related posts about c#

Related posts about sql