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  
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


    (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

    - 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...
    - Disadvantages of the ASP.NET MVC Framework
    - Advantages of the ASP.NET MVC Approach





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