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 6343551
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T20:27:38+00:00 2026-05-24T20:27:38+00:00

I need to populate two drop-down boxes with a list of attached databases to

  • 0

I need to populate two drop-down boxes with a list of attached databases to a server that contain a specifically named table. My current method is this.

List<string> dbType1 = new List<string>();
List<string> dbType2 = new List<string>();
using (var conn = new SqlConnection("Data Source=(local);Integrated Security=true"))
using (var cmd = new SqlCommand())
{
    conn.Open();
    cmd.Connection = conn;

    cmd.CommandText = "select name from sys.databases";
    using (var innerConn = new SqlConnection("Data Source=(local);Integrated Security=true"))
    using (var innerCmd = new SqlCommand())
    using (var rdr = cmd.ExecuteReader())
    {
        innerConn.Open();
        innerCmd.Connection = innerConn;

        while (rdr.Read())
        {
            string table = rdr.GetString(0);
            innerCmd.CommandText = String.Format("select name from [{0}]..sys.tables where name in 'EF_LAB_FIELDS_DYNA' 'AUTOXPAY_PAYMENTS'", table);

            object result = innerCmd.ExecuteScalar();

            if(result != null)
            {
                if ((string)result == "EF_LAB_FIELDS_DYNA")
                    dbType1.Add(table);
                else
                    dbType2.Add(table);
            }
        }

    }

}

cb.Items.AddRange(dbType1.ToArray());
cb2.Items.AddRange(dbType2.ToArray());

This works, however on a server with 205 attached databases it takes 44.6 seconds to run.

Can anyone give me any advice on how to speed this operation up? I am open to using other techniques like SMO, doing more client side processing, or running this as some form of complicated query on the server. As long as I get the two lists based on the two table names it satisfies my needs.


Here is the updated version that brought my run time down to under a second thanks to total’s suggestion.

ConcurrentBag<string> dbType1 = new ConcurrentBag<string>();
ConcurrentBag<string> dbType2 = new ConcurrentBag<string>();
List<string> databases = new List<string>();
using (var conn = new SqlConnection("Data Source=(local);Integrated Security=true"))
using (var cmd = new SqlCommand())
{
    conn.Open();
    cmd.Connection = conn;

    cmd.CommandText = "select name from sys.databases";

    using (var rdr = cmd.ExecuteReader())
    {
        while (rdr.Read())
        {
            databases.Add(rdr.GetString(0));
        }

        Parallel.ForEach(databases, () =>
            {
                var innerConn = new SqlConnection("Data Source=(local);Integrated Security=true");
                var innerCmd = new SqlCommand("", innerConn);
                innerConn.Open();
                return innerCmd;
            },
            (database, loopState, localCommand) =>
            {
                localCommand.CommandText = String.Format("select name from [{0}].sys.tables where name in ('EF_LAB_FIELDS_DYNA', 'AUTOXPAY_PAYMENTS')", database);

                object result = localCommand.ExecuteScalar();

                if (result != null)
                {
                    if ((string)result == "EF_LAB_FIELDS_DYNA")
                        dbType1.Add(database);
                    else
                        dbType2.Add(database);
                }

                return localCommand;
            },
            (localCommand) =>
            {
                var temp = localCommand.Connection;
                localCommand.Dispose();
                temp.Dispose();
            });
    }

}
  • 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-24T20:27:39+00:00Added an answer on May 24, 2026 at 8:27 pm

    I would run each one in a separate thread and then join them all before you continue. You’ll need to use thread safe structures to do this, of course, but this will let you send all your requests at the same time so you don’t have to wait for each one to move on to the next one.

    You might want to use a threadpool instead of one thread for each one if you have 200 something databases, but then again, 200 threads isn’t THAT much… Experiment and profile, but that’s the general approach I would take.

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

Sidebar

Related Questions

I have table with two SelectOneMenu in row. I need populate data in second
I need to insert data from two list (populate from cursors) in the same
If you populate a drop down list of possible values, you only want the
I have a livesearch script that i need to populate my html menu with
i need run code that will create a database and populate tables. i am
I need to add a NOT NULL column to an existing (populated) table that
I need to populate the date time values in two textboxes at a same
I have to pre-populate two calendars with values. I need it to be in
I need to populate a currently empty table with a hundred or so fake
I have MVC3 web application where we need to populate radio button list with

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.