Read the data table content in the new way provided by ado.net 2.0

Posted at : May/21/2007
3666 Views

In ado.net 2.0 version, we can use another way for reading rows in the datatable. Probably, we usually read the rows in datatable by looping its row collection in the previous version ado.net. Now it is like reading the rows in the table via DataReader object. Firstable, you have to create an instance from the datatable object.
You can fill the datatable with DataAdapter or with the Load  method provided by datatable object which require datareader as an input parameter. Now, let's take al look at the sample code below which populate the datatable with Customers rows form Northwind database. Assume that i have a button control which is used for populating the datatable :

   1:  private void btnReadTable_Click(object sender, EventArgs e){
   2:      //fill the datatable    
   3:      using (SqlConnection sqlConn= new SqlConnection
   4:          (@"Database=Northwind2k5;Server=.\sqldev2k5;" +
   5:          "Integrated Security=True"))
   6:          {
   7:  
   8:              SqlCommand sqlCmd = new SqlCommand
   9:                  ("Select * From Customers", sqlConn);
  10:  
  11:              sqlConn.Open();
  12:  
  13:              SqlDataReader sqlDr = sqlCmd.ExecuteReader();
  14:              DataTable dtCust = new DataTable();
  15:   
  16:              dtCust.Load(sqlDr);        sqlDr.Close();
  17:   
  18:              //read the content of the datatable        
  19:              //with the way in ado.net 1.x version:        
  20:              foreach (DataRow aRow in dtCust.Rows)
  21:              {
  22:                  Console.WriteLine(aRow["CustomerID"].ToString());
  23:              }
  24:  
  25:              //read the content of the datatable        
  26:              //with the new alternative way        
  27:              //provided in ado.net 2.0 version:    
  28:              DataTableReader dtReader;
  29:              dtReader = dtCust.CreateDataReader();
  30:              while (dtReader.Read())
  31:              {
  32:                  Console.WriteLine(dtReader["CustomerID"].ToString());
  33:              }
  34:              dtReader.Close();
  35:      }
  36:  }

ABOUT ME

Rully Yulian MF
Rully Yulian Muhammad Firmansyah | Co-Founder & IT Trainer at Native Enterprise | Microsoft Azure Data Scientist | IBM RAG & Agentic AI | IBM Data Science & Data Analyst | Python Certified (PCEP, PCAP) | MOS, MTA, Xamarin Certified, ex MCT | ex MVP

CERTIFICATIONS

Microsoft Certified Associate
IBM RAG and Agentic AI Professional
IBM Data Science Professional IBM Data Analyst Professional
PCAP Associate Python Programmer Certified PCEP Entry Level Python Programmer Certified
Xamarin Certified
MOS 2007
MCPD MCTS
MCAD.NET

NATIVE ENTERPRISE

Native Enterprise - IT Training

FOLLOW ME

Youtube  X Twitter Facebook  Instagram  LinkedIn

RSS


NATIVE ENTERPRISE NEWS

© Copyright 2006 - 2026   Rully Yulian MF   All rights reserved.