SunQuest
 
       MS SQL Server
  Home arrow MS SQL Server arrow Pulling Information Using DataReader With ...
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? 
MS SQL SERVER

Pulling Information Using DataReader With ADO.NET
By: Jagadish Chaterjee
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 10
    2006-04-25

    Table of Contents:
  • Pulling Information Using DataReader With ADO.NET
  • How to retrieve a single value returned by a stored procedure using Data Reader in ADO.NET from ASP.NET: the code
  • How to retrieve a single value returned by a stored procedure using ExecuteScalar
  • How to retrieve a single row returned by a stored procedure using DataReader in ADO.NET from ASP.NET: stored procedure
  • How to retrieve a single row returned by a stored procedure using DataReader in ADO.NET from ASP.NET: the code

  • 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

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Pulling Information Using DataReader With ADO.NET


    (Page 1 of 5 )

    This is the third article in a series focusing on working with stored procedures in ADO.NET. In this article we mainly focus on retrieving single values from stored procedures, retrieving by using "ExecuteScalar," and retrieving an entire row from a stored procedure.
    A downloadable file for this article is available here.

    If you are new to stored procedures (or to accessing them using ADO.NET) in SQL Server, I strongly suggest you go through my first article before proceeding further.

    To work with the stored procedures in this article, you need the simple extra tables “emp” and “dept” within the “Northwind” database.  The structures of those two tables are available in the first article of this series.

    How to retrieve a single value returned by a stored procedure using Data Reader in ADO.NET from ASP.NET: stored procedure

    In my previous article (second in the series), I already explained the RETURN statement in SQL Server stored procedures.  A RETURN statement can only return integer values.  And it is always recommended for technical purposes (like no error, error number, not found, found and so on).  It is not as strongly recommended for other purposes like number of rows, salary of an employee, and so forth.

    In this section, I shall look into the same concept, but with any data type and without using RETURN anymore.  Before going to the ADO.NET code, we need to create a simple stored procedure in SQL Server which returns some value.  Using “Query Analyzer,” execute the following script in the “Northwind” database.

    CREATE PROCEDURE dbo.sp_emp_getName
          (
                @empno int
          )
     
    AS
          SELECT ename FROM emp
                WHERE empno = @empno
          RETURN

    The above stored procedure simply uses a SELECT statement to retrieve an employee name from a table “EMP” based on the “EMPNO” we send through the parameter “@EMPNO.”  The stored procedure is named “sp_Emp_getName.”  Now, we need to go to ADO.NET to access the same in ASP.NET.

    The following are the steps we'll need to take:

    • Create and open a SQL Server connection (using “SQLConnection” object).
    • Create a SQL Server command (using “SQLCommand” object).
    • Specify the properties to the “SQLCommand” object.
    • Assign the “SQLConnection” object to “SQLCommand” object.
    • Execute the stored procedure using “ExecuteNonQuery” method and assign the result to a “SQLDataReader” object.
    • Retrieve the values from the “SQLDataReader” object and place them into your cache.
    • Close “SQLConnection” and release all memory resources.

    The next section shows you how to deal with the ADO.NET code.

    More MS SQL Server Articles
    More By Jagadish Chaterjee


       · Hello guys. You can find all the ways of using DataReader here in this article. ...
     

    MS SQL SERVER ARTICLES

    - Completing the Introduction to Transact-SQL
    - A Brief Introduction to Transact-SQL
    - Lookups and Blocking Bad Data
    - Field Validation Rules for Blocking Bad Data
    - Using Masks to Block Bad Data
    - Blocking Bad Data
    - Using @@ROWCOUNT and TABLE Variables for Dat...
    - How to Use Variables, IF and CASE in Databas...
    - Creating Important Aspects of Notification S...
    - Working wth Variables in Database Interactio...
    - Delving Deeper into Notification Services
    - Notification Services
    - Building a Multi-table Report with SQL 2005 ...
    - A Secure Way of Building Connection Strings
    - Transferring a Database Using the SSIS Desig...





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