Click here to Skip to main content
Click here to Skip to main content
Alternative Tip
Go to top

Accessing Value from System.Data.DataTable : Tip

, 27 Oct 2010
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 4:08 
GeneralDataTable dataTable=new DataTable(); dataTable.C... PinmemberFoyzul Karim28-Oct-10 19:16 
DataTable dataTable=new DataTable();
dataTable.Columns.Add("col1");
dataTable.Columns.Add("col2");
for (int i = 0; i < 100000; i++)
{
dataTable.Rows.Add("value" + i, "name" + i);
}
 
DateTime d1 = DateTime.Now;
foreach (DataRow row in dataTable.Rows)
{
string value1 = row["col1"].ToString();
string value2 = row["col2"].ToString();
string value = value1 + value2;
}
TimeSpan sp1 = DateTime.Now - d1;
Console.WriteLine(sp1.Milliseconds); //output 109 milliseconds
 
DateTime d2 = DateTime.Now;
DataColumn column1 = dataTable.Columns["col1"];
DataColumn column2 = dataTable.Columns["col2"];
foreach (DataRow row in dataTable.Rows)
{
string value1 = row[column1].ToString();
string value2 = row[column2].ToString();
string value = value1 + value2;
}
TimeSpan sp2 = DateTime.Now - d2;
Console.WriteLine(sp2.Milliseconds); //32 milliseconds

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 | Mobile
Web03 | 2.8.140926.1 | Last Updated 27 Oct 2010
Article Copyright 2010 by Andrew Rissing
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid