A more database-agnostic way to get information about the columns of a table in .NET is to use the
GetSchemaTable method of an
IDataReader. Simply perform
ExecuteReader on an SQL statement like
SELECT * FROM source WHERE 0=1
(being sure to protect against SQL injection of course)
and iterate the Rows in the resultant
0=1 ensures that we don't waste bandwidth transferring data needlessly. You can (and should) Close the
IDataReader as soon as you have the
I have found that each ADO.NET provider uses a slightly different order of the columns and some have more columns than others, but the only ones I'm interested in are:
ProviderType needs to be parsed into an appropriate enumeration (e.g.
System.Data.SqlDbType when using SQL Server) to get a meaningful value.
An additional benefit of this technique is that you can list out the columns of a view or ad hoc SQL as well, not just tables.