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

Created at : May/21/2007  
1908 Views   2 Comments

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:  }

[Comments]
fajar Jun/02/2008 06:07:10 :
salam kenal, mas saya lagi buat aplikasi mobile utk PDA aplikasinya akses data, input data ke database... ada contoh codenya ga baik yg untuk Aplikasi Mobilenya maupun Yang untuk webservicenya?? kalau ada tolong kirim dong aku butuh bgt nh... kirim ke fajar_euaggelion@yahoo.com thanx ya...
Rully Jun/02/2008 11:54:40 :
Pada prinsipnya akses data dari PDA atau jenis aplikasi lainnya itu sama, yang berbeda hanya namespace dan batasan class library yang dapat digunakan. Karena PDA itu menggunakan .NET Compact Framework yang merupakan subset dari .NET Framework. Tinggal pelajari konsep ADO.NET nya saja untuk data akses.

[Write your comment]
Name (required)
URL (optional)
Example : http://www.yulianmf.com  
Comment

KZMQ
Input code above below (Case Sensitif) :
About Me 
Rully Yulian MF
My Name is Rully Yulian Muhammad Firmansyah. I am an IT Trainer, IT Consultant and Application Developer spesializing in Microsoft .NET technology and SQL Server database. I live in Bandung, Indonesia. My hobby is to play Guitar. [Read More...]
Top Download 
Bagaimana caranya menginstal database ketika deploying sebuah aplikasi? : Downloaded 2231 times  
Change Group,Sort Order, Filtering By Date in Crystal Reports : Downloaded 2192 times  
Simple Voice Engine Application With Sound Player Class... : Downloaded 1522 times  
DataGridView Grouping : Downloaded 1243 times  
WinForms DataGrid Paging With SqlDataAdapter : Downloaded 1232 times  
Article Category 
Links 
Award 
Certifications 
MOS 2007
MCAS
MCT
MCPD
MCTS
MCAD.NET
ASP.NET Brainbench
Native Enterprise - IT Training 
Native Enterprise Facebook Group 
My Facebook 
My LinkedIn 
Syndication (RSS 2.0) 
Powered By 
Native Enterprise News