Link to home
Start Free TrialLog in
Avatar of andyw27
andyw27

asked on

Accessing data table column name

Hi,

l have a simple loop that loops through all the rows in a data table.  At any point within this loop I need to access the column name?

Can't seem to figure out how this can be done?

Any suggestions?
Avatar of jcrozier21
jcrozier21
Flag of Australia image

I need more information about your environment. Is this a VB.NET app? DAO? C? MS Access? Using a particular activex control?
ASKER CERTIFIED SOLUTION
Avatar of Haris Dulic
Haris Dulic
Flag of Austria image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Well, it depends how do you approach database.
With DataTable you have ColumnName property for each DataColumn
So using DataColumnCollection you could do something like
foreach(DataColumn column in MyDataTable.Columns)
{
    Console.WriteLine(column.ColumnName);
}

Open in new window


If you're using (Sql)DataReader just use GetName method, i.e.

var reader = cmd.ExecuteReader();
for(int i=0;i<reader.FieldCount;i++)
{
   Console.WriteLine(reader.GetName(i));
}

Open in new window

HTH

Ivo Stoykov