[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


How to get column names an types from a sql statement

Posted on 2006-03-31
Medium Priority
Last Modified: 2008-01-09
If a stored procedure that just returns a select statement or an embededded sql:

select * from test_table;

How do you do it in ado.net to figure out the column names and its data types?

For example test_table has the columns first_name varchar(15), mi char(1), last_name varchar(25), date_created datetime.

Question by:borg48

Accepted Solution

osiris247 earned 240 total points
ID: 16345871
If you use a DataAdapter and put the data into a DataTable you can get the column name like this....

DataTable dt = new DataTable("TableName");
SqlDataAdapter adap = new SqlDataAdapter(SqlCommand);
string str = dt.Columns[0].ColumnName; // where 0 is the column index.

get the type...

You can foreach loop through every column if you wish.

hope this helps
LVL 35

Expert Comment

ID: 16346589
SELECT syscolumns.name AS ColName, systypes.name AS ColType, syscolumns.length AS ColSize
FROM sysobjects INNER JOIN syscolumns ON sysobjects.id = syscolumns.id
INNER JOIN systypes ON dbo.syscolumns.xtype = dbo.systypes.xtype
WHERE sysobjects.xtype='U' AND sysobjects.name='Table1'

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Screencast - Getting to Know the Pipeline

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question