Click here to Skip to main content
15,886,654 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i want to get database in asp.net then loop on it to get tables
i know this code but i dont know how to use it

Database db = sv.Databases["Database_Name"]; this to get database
and
TableCollection tables = db.Tables; this to get tables
Posted

Here is the way you get all the tables from a database :

C#
public static List<string> GetDBTables(string connectionString)
{
    using (SqlConnection con = new SqlConnection(connectionString))
    {
        con.Open();
        DataTable dtschema = con.GetSchema("Tables");
        List<string> TableNames = new List<string>();
        foreach (DataRow row in dtschema.Rows)
        {
            TableNames.Add(row[2].ToString());
        }
        return TableNames;
    }
}


Reference

Retrieve List of Tables from Specific Database on Server C#[^]

Hope it helps.
Good luck.
 
Share this answer
 
v5
Comments
Nirav Prabtani 8-Jul-14 4:35am    
When you post code from anywhere then give appropriate reference link in your answer.. :)
Raje_ 8-Jul-14 5:37am    
To be honest, I had this code in my demo solution. I had done this one year back and I might have copied from above link you provided. Thanks any way for editing the solution. :)
Nirav Prabtani 8-Jul-14 6:27am    
Ya ,i am just suggesting you...cheers.. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900