Click here to Skip to main content
15,892,480 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to get names and count of tables in a database from sqlserver and MSAccess?
could u help me pls?
Posted

Count of tables in a database, can be found using the below query:

SQL
SELECT COUNT(*) from information_schema.tables
WHERE table_type = 'base table'
 
Share this answer
 
For Access database :
C#
using System;
using System.Data;
using System.Data.OleDb;

public class DatabaseInfo {    
    public static void Main () { 
        String connect = "Provider=Microsoft.JET.OLEDB.4.0;data source=.\\Employee.mdb";
        OleDbConnection con = new OleDbConnection(connect);
        con.Open();  
        Console.WriteLine("Made the connection to the database");

        Console.WriteLine("Information for each table contains:");
        DataTable tables = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables,new object[]{null,null,null,"TABLE"});

        Console.WriteLine("The tables are:");
            foreach(DataRow row in tables.Rows) 
                Console.Write("  {0}", row[2]);


        con.Close();
    }
}

Extracted from here :
http://stackoverflow.com/questions/4443191/how-do-i-extract-table-names-from-ms-access-database-using-ado[^]

For SQL Server :
C#
public static List<string> GetTables(string connectionString)
{
    using (SqlConnection connection = new SqlConnection(connectionString))
    {
        connection.Open();
        DataTable schema = connection.GetSchema("Tables");
        List<string> TableNames = new List<string>();
        foreach (DataRow row in schema.Rows)
        {
            TableNames.Add(row[2].ToString());
        }
        return TableNames;
    }
}

Extracted from here :
http://stackoverflow.com/questions/6671067/retrieve-list-of-tables-from-specific-database-on-server-c-sharp[^]


Hope it helps.
 
Share this answer
 

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