Accessing Value from System.Data.DataTable : Tip





5.00/5 (3 votes)
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.