Click here to Skip to main content
15,905,612 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
if I want to query the first row, 5th column
I can write like this in ASP.NET:
C#
int columnIndex = 5;
Label1.Text = DataTableObject.Rows[0][columnIndex].ToString();


If I want to know the 5th column's field name?How can I get it?

And how can I get a Table name from database?
Posted

Hi Johnson,

Yes you can, using column.ColumnName, like this

For example, to retrieve the name of the 5th column from the DataTable object,

String colName = dtBooks.Columns[4].ColumnName ;

Note that the column index starts with 0.

To get table name from datatable you can use
C#
private void GetTableNames(DataSet dataSet)
{
    // Print each table's TableName. 
    foreach(DataTable table in dataSet.Tables)
    {
        Console.WriteLine(table.TableName);
    }
}

Hope this helps you a bit.

Regards,
RK
 
Share this answer
 
This will work on sql server 2005 and up:
SQL
select * from INFORMATION_SCHEMA.COLUMNS
where TABLE_Name='YourTableName'
order by ORDINAL_POSITION


further if you required information in details you can visit below mentioned links.
1. Link[^]
2. Link1[^]
3. Link[^]
4. Link[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900