Thursday, April 17, 2014

How to get name of all tables in ms sql database aspnet c#

public System.Collections.Generic.Dictionary<stringstring> GetAllTables(System.Data.SqlClient.SqlConnection _connection)
{
    if (_connection.State == System.Data.ConnectionState.Closed)
        _connection.Open();
    System.Data.DataTable dt = _connection.GetSchema("Tables");
    System.Collections.Generic.Dictionary<stringstring> tables = new System.Collections.Generic.Dictionary<stringstring>();
    foreach (System.Data.DataRow row in dt.Rows)
    {
        if (row[1].ToString().Equals("BASE TABLE"StringComparison.OrdinalIgnoreCase)) //ignore views
        {
            string tableName = row[2].ToString();
            string schema = row[1].ToString();
            tables.Add(tableName, schema);
        }
    }
    _connection.Close();
    return tables;
}