Click here to Skip to main content
Click here to Skip to main content
Alternative Tip/Trick

Accessing Value from System.Data.DataTable : Tip

, 27 Oct 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
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)

Share

About the Author

Andrew Rissing
Software Developer (Senior)
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... PinmemberAndrew Rissing29-Oct-10 5:08 
GeneralDataTable dataTable=new DataTable(); dataTable.C... PinmemberFoyzul Karim28-Oct-10 20:16 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.141223.1 | Last Updated 27 Oct 2010
Article Copyright 2010 by Andrew Rissing
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid