et said:
How do I get the names of the tables in a database via ado.net? Once
I have the table names, how do I get the names of the Fields for each
table?
I've been writing code to do exactly this over the last few days. As has
already been mentioned, the solution depends in part upon which database
objects you're using.
If you're using the SqlClient objects, you can execute the following SQL
statement to get the tables:
SELECT * FROM Information_Schema.Tables
WHERE Table_Type = 'BASE TABLE'
Note however that this will also return the "dtproperties" table, which you
probably don't want so you can modify the query as follows to work around
this:
SELECT * FROM Information_Schema.Tables
WHERE Table_Type = 'BASE TABLE'
AND OBJECTPROPERTY(OBJECT_ID(table_name),'IsMSShipped')=0
If you're using OleDbClient objects, you can use this to get the tables:
Dim dt as DataTable
dt = Connection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, New String()
{Nothing, Nothing, Nothing, "TABLE"})
In both cases, to get the list of fields I used the FillSchema method of the
DataAdapter object. This returns all the column information for a query
(SELECT * FROM TABLE) so that it can be interrogated using the Columns
collection of the appropriate Table object within the DataSet.
Hope that helps,