How to get column names an types from a sql statement

Posted on 2006-03-31
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 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
    LVL 7

    Accepted Solution

    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

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

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Join & Write a Comment

    Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
    This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!

    755 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

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now