Getting table schema information with ADO.NET

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Given a table name (e.g. CUSTOMERS), how do I get column names of the table
and the data types of the columns the table has?

I am using SQLServer and would like to get that info from my C# code.
 
Given a table name (e.g. CUSTOMERS), how do I get column names of the
table
and the data types of the columns the table has?

I am using SQLServer and would like to get that info from my C# code.

I am sure there are more elegant ways, but you could always read "EXEC
sp_help '<tablename>'" into an SqlDataReader and look at the second
Resultset... See BOL for further info on sp_help
 
Use the INFORMATION_SCHEMA views to access the schema information:

SELECT
COLUMN_NAME,
DATA_TYPE,
CHARACTER_MAXIMUM_LENGTH
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'Customers'

This will return all the column names, data type and length for character
data, one row per column.

I hope this helps,

Jon
 
Back
Top