Click here to Skip to main content
15,886,110 members
Articles / General Programming / Performance
Alternative
Tip/Trick

Accessing Value from System.Data.DataTable : Tip

Rate me:
Please Sign up or sign in to vote.
5.00/5 (3 votes)
27 Oct 2010CPOL 13.6K   2   2
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)


Written By
Architect
United States United States
Since I've begun my profession as a software developer, I've learned one important fact - change is inevitable. Requirements change, code changes, and life changes.

So..If you're not moving forward, you're moving backwards.

Comments and Discussions

 
GeneralI personally like using Stopwatch over DateTime, but that'll... Pin
Andrew Rissing29-Oct-10 4:08
Andrew Rissing29-Oct-10 4:08 
GeneralDataTable dataTable=new DataTable(); dataTable.C... Pin
Foyzul Karim28-Oct-10 19:16
professionalFoyzul Karim28-Oct-10 19:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.