Retrieving Column names from database

I am writing an application in C# that uses tables to store some of its data. I am trying to retrieve the column names from the database so they can be outputted to the user. Thus the user can change what is outputted to the screen just by editing the table and not changing any code. How do I use a stored procedure to access the database and retrieve specific column names An example table would look like this:

Name Color Size
--------------------------------------
S1 red large
S2 yellow small
S3 blue medium

How can I retrieve the header "color", using SQL stored Procedures, to be used in my c# program

Thanks,
Ashley



Answer this question

Retrieving Column names from database

  • Nina and team

    If color is the actual name of the table in the database, then it will pass through on your SQL query.

    E.g. Select * from Color;

    If it's a column and it's named color then the same thing will happen.

    E.g. SELECT Color from MyTable;

    If it's not named Color, then you can use the 'AS' clause to rename it

    E.g. SELECT MyColumn AS Color From MyTable;

    Hope this helps

    Colin Brown
    Windows Live MVP



  • Peter Mackay

    I think what she is looking for is how to extract the column name from the recordset.

    There's not alot to go on, but i'm assuming you've loaded the table into a dataset. So this would extract a column name from the dataset

    ds.Tables[0].Columns[0].ColumnName()

    where ds is the dataset object, this would extract the name of the first column from the first table in the dataset.



  • Retrieving Column names from database