GetSchema and DbProviderFactories - List of Tables in a Database

I am slowly getting back into the swing of things. Today was my first chance in a couple of weeks to get back into the MSDN Forums.

One of my favorite subjects came up today, GetSchema, which I have talked about a number of times. GetSchema is one of those great new features in ADO.NET 2.0 to help with pulling metadata and information from a database.  You can use it generically with DbProviderFactories and DbConnection to work against any .NET Managed Database Provider. Below is an example using GetSchema to get a list of tables from the Northwind Database:

 

DbProviderFactory factory =
DbProviderFactories.GetFactory("System.Data.SqlClient"); DataTable tables = null; using (DbConnection connection = factory.CreateConnection()) { connection.ConnectionString = "Data Source=(local);
Initial Catalog=Northwind;Integrated Security=True
"; string[] restrictions = new string[4]; // Catalog restrictions[0] = "Northwind"; // Owner restrictions[1] = "dbo"; // Table - We want all, so null restrictions[2] = null; // Table Type - Only tables and not views restrictions[3] = "BASE TABLE"; connection.Open(); // Here is my list of tables tables = connection.GetSchema("Tables", restrictions); }

 

Pretty simple stuff once you get used to it.

Source: David Hayden ( .NET Developer )

 

ADO.NET 2.0 Tutorials

 

posted on Wednesday, March 29, 2006 6:07 PM

Main

News

Green Tea

.NET Development

Enterprise Library

Patterns & Practices