Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 839609
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T05:29:32+00:00 2026-05-15T05:29:32+00:00

I have a program that stores user projects as databases. Naturally, the program should

  • 0

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);
      }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T05:29:33+00:00Added an answer on May 15, 2026 at 5:29 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 496k
  • Answers 496k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer This is the documentation I use. Be warned though, the… May 16, 2026 at 11:37 am
  • Editorial Team
    Editorial Team added an answer You can't use cmd or login if you can't get… May 16, 2026 at 11:37 am
  • Editorial Team
    Editorial Team added an answer You should be able to use the following code: if… May 16, 2026 at 11:37 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I have a DAL class library that is included in my program as a
I have a java program that I am trying to generate 3 outputs for,
I have a project where a user for the company will use a winform
I would like to create the ASP.NET User database template on a database of
I have a WiX project that installs a few EXE files. One is the
I am trying to have the function toggleClick return powerData. I need to store
I am new to assembly language. I have written a program for taking an
I have built a console application that works okay when it references a .exe
I realize that the answer to this question is likely quite obvious (if somewhat
I have been working on many applications which run as windows service or scheduled

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.