Database
  Home arrow Database arrow Page 6 - ADO.NET 101: SqlDataReader
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 
Mobile Linux 
App Generation ROI 
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? 
DATABASE

ADO.NET 101: SqlDataReader
By: Jayaram Krishnaswamy
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 11
    2005-03-15

    Table of Contents:
  • ADO.NET 101: SqlDataReader
  • SQLDataReader Class
  • SQLCommand Execution
  • SQLCommand for CommandType: SQL Statement
  • SQLCommand for CommandType: Stored Procedure
  • Stored Procedure with one input and one output parameter
  • SQLCommand's ExecuteXMLReader() Method

  • 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


    ADO.NET 101: SqlDataReader - Stored Procedure with one input and one output parameter


    (Page 6 of 7 )

    This Stored Procedure YtdSales requires an input variable @title and the result of the query is returned in another variable @ytd_sales. Notice that the OUTPUT variable is clearly marked in the Stored Procedure. Also note the ParameterDirection attribute.

    Create Procedure YtdSales
    @title varchar(50), @ytd_sales int OUTPUT
    as
    Select @ytd_sales=ytd_sales
    from titles
    where title=@title
    return

    1. Establish a connection as before

    2. Place a textbox to receive the input value, a TextBox to display the output and a button to initiate retrieval.

    3. In the click event of the button insert the code shown in step 4.

    4. In this case there is no need to loop the returned rows as there will be only one result returned.

       Private Sub Button1_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Button1.Click
            'declare a connection of type SQLConnection
            Dim SqlConnection1 = New SqlClient.SqlConnection
            'define the connection string for this connection
            SqlConnection1. _
            connectionString = "workstation id= XPHTEK;" & _
            "packet size=4096;" & _
                "integrated security=SSPI;data source=XPHTEK;" & _
                "persist security info=False;initial catalog=pubs"
            'open the connection
            SqlConnection1.Open()
            'declare and define the SQLCommand to process a
            'stored procedure
            Dim cmd As New SqlClient.SqlCommand
            With cmd
                .Connection = SqlConnection1
                .CommandType = CommandType.StoredProcedure
                .CommandText = "YtdSales"
            End With
            'declare a new SQLParamter
           
    Dim param1 As New SqlClient.SqlParameter
            'add this parameter to the collection
           
    param1 = cmd.Parameters.Add _
            ("@title", SqlDbType.VarChar)
            'this parameter is the input
            param1.Direction = ParameterDirection.Input
            'declare another paramter
           
    Dim param2 As New SqlClient.SqlParameter
            'add this second parameter to the collection
           
    param2 = cmd.Parameters.Add _
            ("@ytd_sales", SqlDbType.Int)
            'this one is the output
            param2.Direction = ParameterDirection.Output
            'assign the input parameter
            cmd.Parameters("@title").Value = "Is Anger the Enemy?"

            Dim dr As SqlClient.SqlDataReader
            'Perform ExecuteReader()
            dr = cmd.ExecuteReader()
            '
            
    TextBox1.Text = param1.Value
              TextBox2.Text = param2.Value
            Response.Write(cmd.Parameters.Count())
        End Sub

     
    5. When the code is run you should see the following:

    ADO.NET 101: SqlDataReader

    More Database Articles
    More By Jayaram Krishnaswamy


     

    DATABASE ARTICLES

    - Database Programming in C# with MySQL : Usin...
    - Formatting Techniques for Data Access from E...
    - Data Access from Excel VBA
    - Generating a Multiple Table Crystal Report u...
    - ADO and the Command Object
    - On Wiring Up an ADO Data Control
    - Reading and Writing to Files on the Intranet
    - Using ADO Record to Create and Navigate Intr...
    - Using Data Access Pages to Access Data on a ...
    - Using ADO with the SQL Native Client
    - ADO`s Stream Object
    - Opening a Record Object Referencing an Open ...
    - Introducing Jasper (SQL Anywhere 10 Beta)
    - Creating a Database Project in VS 2005
    - Manipulating ADO Recordsets





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