ASP.NET
  Home arrow ASP.NET arrow Page 4 - 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 
Moblin 
JMSL Numerical Library 
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
     
     
    ADVERTISEMENT


    Using the SqlDataReader Class - More about the SqlDataReader Indexers


    (Page 4 of 6 )

    The implementation of the column-ordinal based indexer of the SqlDataReader class looks as follows:

    public override object this[int i]

    {

      get

    {

      return this.GetValue(i);

     }

    }

    And the SqlDataReader's column-name based indexer implementation looks like this:

      public override object this[string name]

    {

      get

    {

       return this.GetValue(this.GetOrdinal(name));

     }

    }
     

    As you might have noted in the code example, we have used the ToString() method with the SqlDataReader's returned fields to create the ListItem object.

    ListItem item = new ListItem(dataReader[1].ToString(), dataReader[0].ToString());

    // or you can replace the above line of code with the following one

    //ListItem item = new ListItem(dataReader["job_desc"].ToString(),

    // dataReader["job_id"].ToString());

    Both the column-ordinal based indexer and the column-name based indexer return an object data type so in our code we needed to call the SqlDataReader[Column Name or Column Ordinal].ToString() method to return a string value to create the List Item. What you should note about those indexers is the methods they use to return the field you are asking for, namely the GetValue() method and the GetOrdinal() method. The GetValue() method is used to get the field's value. It accepts the index of the field and returns it. In the column-name indexer the SqlDataReader uses the syntax this.GetValue(this.GetOrdinal(name)) in order to return the value of the field. The GetValue() method returns the field as an object data type too.

    The SqlDataReader.GetOrdinal() method accepts a string value, which is the column name, and returns its ordinal. The GetOrdinal() method's return value is used as an argument to the SqlDataReader.GetValue() method, that accepts the column ordinal, to get the value of the field.

    I think by now you have a better understanding of how these indexers return the field's data and you also know that using the column-ordinal indexer is faster than using the column-name indexer.

    We may also use the methods that the indexers use directly, as in the following example.

    More ASP.NET Articles
    More By Michael Youssef


       · Hi guys,Your comments are welcome.
     

    ASP.NET ARTICLES

    - Retrieving Data with AJAX and the GridView C...
    - Playing with Images in ASP.NET 3.5 AJAX Appl...
    - Saving and Retrieving Data with AJAX
    - 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...





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