• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2041
  • Last Modified:

C# how to fetch tables list from sql database

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

conn.Open();

 

 

 


//Show all Databases From the Selected SQL Server

 

SqlCommand SqlCom = new SqlCommand();

SqlCom.Connection = conn;

SqlCom.CommandType =


CommandType.StoredProcedure;

SqlCom.CommandText =


"sp_databases";

 


SqlDataReader SqlDR;

SqlDR = SqlCom.ExecuteReader();

 


while (SqlDR.Read())

{

cbDBList.Items.Add(SqlDR.GetString(0));

}

0
pixels123
Asked:
pixels123
  • 3
1 Solution
 
srikanthreddyn143Commented:
SELECT * FROM INFORMATION_SCHEMA.TABLES

use the above query. Before that based on database selection you have to change the connection string.
0
 
Sreedhar VengalaCommented:
Do bit of research on SMO (SQL Mangement Objects),which is pretty straight to all with SQL server from .net Code
0
 
Sreedhar VengalaCommented:
0
 
HarryNSCommented:
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
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now