65.9K
CodeProject is changing. Read more.
Home

Accessing Value from System.Data.DataTable : Tip

starIconstarIconstarIconstarIconstarIcon

5.00/5 (3 votes)

Oct 26, 2010

CPOL
viewsIcon

14341

Technically, the best approach here for speed and flexibility is to do the following:public class TestClass{ private const string EMP_ID = "EmpId"; public void MyTestMethod() { //GetData fetches data from the database using a SQL query DataTable dt =...

Technically, the best approach here for speed and flexibility is to do the following:
public class TestClass
{
    private const string EMP_ID = "EmpId";
    public void MyTestMethod()
    {
        //GetData fetches data from the database using a SQL query
        DataTable dt = DataAccess.GetTableData();
        DataColumn dcEmpId = dt.Columns[EMP_ID];
        foreach (DataRow dRow in dt.Rows)
        {
            //Accessing data through datacolumn
            int empId = Convert.ToInt32(dRow[dcEmpId]);
        }
    }
}
Accessing data from a DataTable is actually fastest when using the DataColumn.