Home arrow ADO.NET arrow Page 3 - Data Access in .NET using C#: Part 1
ADO.NET

Data Access in .NET using C#: Part 1


ADO.NET and C# can be used together to work with an Access database to produce a simple application. In this article Sachin shows us how it's done.

Author Info:
By: Sachin Korgaonkar
Rating: 4 stars4 stars4 stars4 stars4 stars / 164
November 15, 2002
TABLE OF CONTENTS:
  1. · Data Access in .NET using C#: Part 1
  2. · Data Access in .NET
  3. · The DataReader Object
  4. · Conclucion

print this article
SEARCH DEVARTICLES

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.
blog comments powered by Disqus
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

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials