C# how to fetch tables list from sql database

Posted on 2009-04-23
Last Modified: 2013-12-17
I am developing a windows form application in which i need to display all databasees available into a combo box and when select a database form combo box then it dispalys the available database tables list into another combobox.

any idea how to do it. I get the list of databases from sql server by following code.


SqlConnection conn = new SqlConnection("Data Source=LW-AN\\SQLEXPRESS;Persist Security Info=True;User ID=sa;Password=App1234");





//Show all Databases From the Selected SQL Server


SqlCommand SqlCom = new SqlCommand();

SqlCom.Connection = conn;

SqlCom.CommandType =


SqlCom.CommandText =



SqlDataReader SqlDR;

SqlDR = SqlCom.ExecuteReader();


while (SqlDR.Read())




Question by:pixels123
    LVL 11

    Expert Comment


    use the above query. Before that based on database selection you have to change the connection string.
    LVL 9

    Expert Comment

    by:Sreedhar Vengala
    Do bit of research on SMO (SQL Mangement Objects),which is pretty straight to all with SQL server from .net Code
    LVL 9

    Accepted Solution

    LVL 9

    Expert Comment

    by:Sreedhar Vengala
    LVL 6

    Expert Comment

    Use any one of the following statement to get list of databases...

    EXEC sp_databases
    EXEC sp_helpdb
    SELECT name FROM sys.databases
    SELECT name FROM sys.sysdatabases

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Suggested Solutions

    This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
    A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    9 Experts available now in Live!

    Get 1:1 Help Now