MS SQL Server
  Home arrow MS SQL Server arrow Page 3 - 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 
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? 
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 / 12
    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


    Pulling Information Using DataReader With ADO.NET - How to retrieve a single value returned by a stored procedure using ExecuteScalar


    (Page 3 of 5 )

    In the previous section, we already worked on returning single values and handling them using “DataReader.”  There is one more method we need to learn.  We can use the “ExecuteScalar” method of the “SQLCommand” object to do exactly the same thing.

    We shall work with the same stored procedure we worked with in the previous section.  But the steps will be a bit different.

    The following are the steps required to execute a simple stored procedure with ADO.NET:

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

    Now we shall just follow the above steps practically.  Add a new web form (call it  “SPReturningValueUsingScalar”) with a single button captioned “Retrieve Single Value” and a label named “lblEname” (similar to the previous section).

    After designing the form, switch to the code and add the following line at the top.

    Imports System.Data.SqlClient

    Add the following code to your “Retrieve Single Value” button:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
    As System.EventArgs) Handles Button1.Click
            Dim name As String
            Dim cn As New SqlConnection("Data Source=.;initial
    catalog=Northwind;user id=sa")
            Dim cmd As New SqlCommand
            With cmd
                .CommandType = CommandType.StoredProcedure
                .CommandText = "sp_emp_getName"
                .Parameters.Add("@empno", 1201)
                .Connection = cn
                .Connection.Open()
                name = .ExecuteScalar & ""
                'release resources
                .Connection.Close()
                .Dispose()
            End With
            Me.lblEname.Text = IIf(name.Length = 0, "Not Found",
    name)
        End Sub

    The second method is very easy and very efficient.  But it works with only one value.  A “DataReader” can be used to retrieve one value, one row or even more rows continuously. 

    The limitation with “ExecuteScalar” is that it can always retrieve only one value (but not a row or rows).  The “ExecuteScalar” is specially designed and tuned to retrieve single values.  It is the best in performance when compared with “DataReader” when retrieving single value.

    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 4 hosted by Hostway