Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / programming / performance

Accessing Value from System.Data.DataTable : Tip

5.00/5 (3 votes)
27 Oct 2010CPOL 14.3K  
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.

License

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