views:

64

answers:

2

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);
      }
    }
}
+2  A: 

Two comments. First off, you should use a using statement and your could will be much cleaner.

More on topic, you are connecting to the database when you are trying to drop it! Connect to the master database instead.

John Buchanan
I don't that is the problem. I've tried connecting to the master db before deleting it, and it didn't fix anything. It works if don't call BuildProjectList.
Everett
+1  A: 

The SqlConnection class polls the actual database connection. If you close the SqlConnection, the connection is returned to the Connection pool. To prevent this behaviour, set SqlConnection.Pooling = false;.

edit

John seems to be more to the point here. But you might have to keep polling in mind as well.

Femaref
I couldn't find the SQLConnection.Pooling property, but I tried calling SQLConnection.ClearAllPools() before deleting the database and that did the trick. Thanks!
Everett