ADO.NET
  Home arrow ADO.NET arrow Page 3 - Data Access in .NET using C#: Part 1
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Dedicated Servers  
Actuate Whitepapers 
Moblin 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
ADO.NET

Data Access in .NET using C#: Part 1
By: Sachin Korgaonkar
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 79
    2002-11-15

    Table of Contents:
  • Data Access in .NET using C#: Part 1
  • Data Access in .NET
  • The DataReader Object
  • Conclucion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Data Access in .NET using C#: Part 1 - The DataReader Object


    (Page 3 of 4 )

    We can use a DataReader object to read data from a database. A DataReader object is effectively a forward-only collection of records from your data source. The problem is, you can't go back because the recording is forward only. A DataReader cannot be used to write or modify data. When creating a DataReader, we first create a command object from the connection object and then initialize this object using CommandText property:

    OleDbDataReader dbReader = null;
    OleDbCommand cmd = Myconnection.CreateCommand();

    cmd.CommandText = "SELECT * FROM familyTree";
    dbReader = cmd.ExecuteReader();


    The DataReader object is created by calling the ExecuteReader method on the Command Object.

    while(dbReader.Read())

    {
    Email = (string)dbReader.GetValue(5);
    lb.Items.Add(Email);
    }


    The DataReader object exposes a Read() method. The Read() method automatically points to the next row in the database table that we are working with. When there are no more rows, the Read() method will return false. In the code above we are using a simple while loop to check the table data.

    In the body of the while loop, the Read() method returns and adds the email field from the table into our list box control. Press Ctrl + F5 for build solutions and execute our application without debugging.

    Click on the button to connect to the database and retrieve the email address. The email address from our database table should appear in the list box on our form.

    Finally, always call the Close() method when you are finished with the database. This will free up the memory used previously by the database objects. The garbage collector will do this for us automatically, but in its own time. In the meantime, valuable resources are being taking up. Until the garbage collector comes around and removes the connections -- freeing resources -- the database connection will still be open, which is a waste of resources and a bad programming practice.

    More ADO.NET Articles
    More By Sachin Korgaonkar


     

    ADO.NET ARTICLES

    - Datasets in Microsoft.Net
    - Latest Developments in the .Net World
    - Introduction to .NET
    - Automatic Generation of Single Table SQL Sta...
    - Data Access in .NET using C#: Part 1
    - All You Need To Know About ADO.NET: Part 2/2
    - All You Need To Know About ADO.NET: Part 1/2
    - Easing Transition From ASP and ADO to ASP.NE...
    - A Practical Comparison of ADO and ADO.NET


    Iron Speed





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway