Get List of Tables in a Database - Query INFORMATION_SCHEMA.Tables - ADO.NET
by David Hayden ( Florida .NET Developer )
Someone asked how to get a list of tables in a database today in the MSDN Visual C# Forum:
http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=227746&SiteID=1
My initial reaction was to discuss all the cool stuff I have been doing recently around SQL Server Management Objects and GetSchema as mentioned in the following posts:
I'll admit it. Sometimes I get a little over excited in my answers, because I love a lot of the new features in ADO.NET 2.0 as well as all the goodies that come with SQL Server 2005.
Turns out the person needed it for .NET 1.1 :) hehe... Alright, to get a list of tables in a database using .NET 1.1 and C# ( works in ADO.NET 2.0 and C# 2.0 as well ) we need to query INFORMATION_SCHEMA.Tables in the database as such:
Update: I just realized that the DataTable stuff I am doing below is only possible in ADO.NET 2.0, but you get the idea and can work around it :)
string connectionString = "...";
DataTable tables = new DataTable("Tables");
using (SqlConnection connection =
new SqlConnection(connectionString))
{
SqlCommand command = connection.CreateCommand();
command.CommandText = "select table_name as Name from
INFORMATION_SCHEMA.Tables where TABLE_TYPE =
'BASE TABLE'";
connection.Open();
tables.Load(command.ExecuteReader(
CommandBehavior.CloseConnection));
}
The names will be in the tables DataTable.
Source: David Hayden ( Florida .NET Developer )
ADO.NET 2.0 Tutorials