简体   繁体   中英

Why are there connections open to my databases?

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);
      }
    }
}

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM