How do you get a specific value from a System.Data.DataTable?

I am a low-level algorithm programmer, and databases are not really my stuff, so this will be question n00b if it has ever been.

I execute a simple SELECT query through our DAO development team. DAO returns a System.Data.DataTable object containing the query results. So far, everything is working.

The problem I am facing now:
I need to infer a value from one of the fields of the first row in the resulting DataTable, and I don’t know where to start. Microsoft is so confused about this! Arrrg!

Any advice would be appreciated. I do not provide any code samples because I think the context here is superfluous. I assume that all DataTable objects work the same way no matter how you run your queries, and therefore any additional information will just make it more confusing for everyone.

+5
source share
3 answers

Just the basics ...

yourDataTable.Rows[ndx][column]

where ndx is the row number (starting from 0) where the column can be a DataColumn object, index (column n) or column name (row)

yourDataTable.Rows[0][0]
yourDataTable.Rows[0][ColumObject]
yourDataTable.Rows[0]["ColumnName"]

check null value, compare with DBNull.Value;

+7
source

Do you mean how table.Rows[0]["MyColumnName"]?

+1
source

ID = 5 (.. ) .

DataRow dr = myDataTable.Rows.Find(5);
String s = dr["Description"].ToString();
0

All Articles