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  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
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 / 4
    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

    - Develop Your First ASP.NET Website with Visu...
    - Run ASP.NET in Windows XP Home with Cassini ...
    - How to Test a Web Application
    - How to Add Code and Validation Controls to a...
    - Working in Source and Split Views to Build a...
    - How to Build a Web Form for a One-Page Web A...
    - How to Develop a One-Page Web Application
    - An ASP.NET Web Application in Action
    - Developing ASP.NET Web Applications
    - An Introduction to ASP.NET Web Programming
    - Introduction to the ADO.NET Entity Framework...
    - Completing an In-Text Advertising System und...
    - Programming an In-Text Advertising System un...
    - Building an In-Text Advertising System Under...
    - Developing a Mini ASP.NET AJAX Server Centri...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    Stay green...Green IT