ASP.NET
  Home arrow ASP.NET arrow Using the SqlDataReader Class
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Dedicated Servers 
Actuate Whitepapers 
VeriSign Whitepapers 
Windows Web Hosting
 
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? 
ASP.NET

Using the SqlDataReader Class
By: Michael Youssef
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 3
    2007-10-24

    Table of Contents:
  • Using the SqlDataReader Class
  • Retrieving data from the Pubs Database
  • Understanding the code example
  • More about the SqlDataReader Indexers
  • Using the SqlDataReader.GetValue() and GetOrdinal() methods
  • Returning Fields in appropriate data types

  • 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
     
    IBM developerWorks
     
    ADVERTISEMENT

    At the virtual BlackBerry Technical Seminar 2008, you can ask your development questions directly of Research In Motion® (RIM) experts, and take advantage of learning opportunities designed uniquely for BlackBerry solution developers. Register Today!

    Using the SqlDataReader Class


    (Page 1 of 6 )

    If you read any of my previous ADO.NET articles you will find that I used the SqlDataReader class in many of the examples. Today we will talk more about the SqlDataReader class and use its methods and properties. Some of you may wonder how the SqlDataReader class gets access to the fields using the array-like syntax; we will talk about that as well.

    If you are not familiar with C# Indexers, please consult my article for a Behind the Scenes Look at C#: Indexers.

    If you are not familiar with the SqlDataReader class, you should know that it's one of the players on the ADO.NET team of data access classes. We have the SqlConnection class, which is used to create and open a connection to the database using the connection string that you provide, and we have the SqlCommand class, which is used to execute a T-SQL statement, like SELECT, INSERT, UPDATE or DELETE and stored procedures against a database using the connection opened by the SqlConnection object, and retrieve the result.

    As you might know, one of the methods of the SqlCommand class is used to retrieve an object of type SqlDataReader; this is the SqlCommand.ExecuteReader() method. The SqlDataReader is what you are going to use to retrieve data -- to the client, for example, or to a business component like the ObjectDataSource control, which we are going to discuss in the next few articles along with the SqlDataSource class.

    The SqlDataReader retrieves the data in a read-only forward-only mechanism. Read-only means that you can't use the SqlDataReader class to execute statements that update the data in your databases. Forward-only means that you don't have access to the previous row once you have read it; the SqlDataReader class retrieves the row, discards it, and then retrieves the next row and so on. You have access to the fields of the current row using indexers.

    A column-ordinal based indexer uses the column number to retrieve the field's data, as in myDataReader[0]. And a string-based indexer uses the column name to access its field's value. Usually, you would use the string column-name based indexer because it eliminates any confusion about what field you are retrieving. You would write something like myDataReader["LastName"]. But it's faster to use the column-ordinal based indexer because the data reader will not perform any additional operations to retrieve the field's value; it will just go to the column ordinal you want to access and get the value. With the string column-name based indexer, however, as in myDataReader["LastName"], the SqlDataReader has to search for a column with that name, and then returns its value, which slows the performance a bit.

    Let's see an example that uses the SqlDataReader class. Note that we are using the SQL Server .NET Data Provider for all the examples.

    More ASP.NET Articles
    More By Michael Youssef


       · Hi guys,Your comments are welcome.
     

    ASP.NET ARTICLES

    - Enhancing PHP Via the ASP.NET AJAX Framework...
    - Enhancing PHP Programming with the ASP.NET A...
    - Classes and ASP.NET AJAX
    - Using ASP.NET AJAX
    - Building a Simple Storefront with LINQ
    - Developing a Dice Game Using ASP.NET Futures...
    - Completing an ASP.NET AJAX Server-Centric Ba...
    - Information Management for an ASP.NET AJAX S...
    - Comment and Order Management for an ASP.NET ...
    - Back-end Management Tasks for an ASP.NET AJA...
    - User Information Management for an ASP.NET A...
    - Adding Comments and Search to an ASP.NET AJA...
    - Order-Related Modules for an ASP.NET AJAX Se...
    - User and Role Management for an ASP.NET AJAX...
    - Programming an ASP.NET AJAX Server-Centric B...

    Iron Speed




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