Connecting Tech Pros Worldwide Forums | Help | Site Map

Getting table schema information with ADO.NET

Jason Manfield
Guest
 
Posts: n/a
#1: Nov 17 '05
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.

Mark Rae
Guest
 
Posts: n/a
#2: Nov 17 '05

re: Getting table schema information with ADO.NET


"Jason Manfield" <JasonManfield@discussions.microsoft.com> wrote in message
news:39FB49EC-F2EB-44B9-80AC-212E43CECC59@microsoft.com...
[color=blue]
> 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.[/color]

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


CodeMeister
Guest
 
Posts: n/a
#3: Nov 17 '05

re: Getting table schema information with ADO.NET


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

"Jason Manfield" <JasonManfield@discussions.microsoft.com> wrote in message
news:39FB49EC-F2EB-44B9-80AC-212E43CECC59@microsoft.com...[color=blue]
> 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.[/color]


W.G. Ryan eMVP
Guest
 
Posts: n/a
#4: Nov 17 '05

re: Getting table schema information with ADO.NET


You can use the GetSchemaTalbe method of a SqlDataReader or loop through a
DataTable that's been filled with an Adapter.

http://www.knowdotnet.com/articles/g...hematable.html
should help

--
W.G. Ryan MVP (Windows Embedded)

TiBA Solutions
www.tibasolutions.com | www.devbuzz.com | www.knowdotnet.com
"Jason Manfield" <JasonManfield@discussions.microsoft.com> wrote in message
news:39FB49EC-F2EB-44B9-80AC-212E43CECC59@microsoft.com...[color=blue]
> Given a table name (e.g. CUSTOMERS), how do I get column names of the[/color]
table[color=blue]
> 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.[/color]


Closed Thread