Database
  Home arrow Database arrow Page 4 - 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 - SQLCommand for CommandType: SQL Statement


    (Page 4 of 7 )

    The following code makes a SQLConnection using the machine/database specific connection string, and prepares a SQLCommand to run a SQL Select Statement against the database. Observe that the code is along the lines of the recipe mentioned above. The SQLDataReader gets assigned when the SQLCommand's ExecuteReader() method is carried out.

    In the click event of a button placed on a Web page, run the following code after building it. The result is written to the browser screen. SQLDataReader's HasRows property can be put to good use to determine if any rows are returned; the FieldCount Property can be used to find the number of columns returned, and so forth. In any case, the speed of retrieval can be improved if the data types returned are known beforehand.

     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 connction string of 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 a SQLCommand1 as the new instance
            Dim SQLCommand1 = New SqlClient.SqlCommand
            'set the next four properties of the SQLCommand
            SQLCommand1.Connection = SqlConnection1
            SQLCommand1.CommandTimeout = 30
            SQLCommand1.CommandType = CommandType.Text
            'this property is the query that is run against
            'the(database)
            SQLCommand1.CommandText = "SELECT emp_id, " & _
            "fname, lname, hire_date FROM employee " & _
            " WHERE (hire_date > '1/1/1993')"
            'declare a SQLDataReader
            Dim dr As SqlClient.SqlDataReader
            'carry out the ExecuteReader method of the
            'command object
            dr = SQLCommand1.ExecuteReader
            'prints the number of columns in the retrieved data
            Response.Write("<b>No of Columns Retrieved: </b>" & _
            dr.FieldCount & "<br/>")
            'prints a Boolean if rows are returned
            Response.Write("<b>Are rows returned from query?: </b>" & _
            dr.HasRows & "<br/>")
            'verifies with a Boolean whether the reader is
            'closed or open
            'here, SQLDataReader is open
            Response.Write("<b>Is the SqlDataReader closed ?</b>" & _
            dr.IsClosed & "<br/>")
            While dr.Read
                'here you retrieve the row pertaining to the first
                'column()
                Response.Write(dr.Item(1) & "<br/>")
            End While
            'close the SQLDataReader
            dr.Close()
            'Here it is closed
            Response.Write("<b>Is the SqlDataReader closed ?</b>" & _
            dr.IsClosed & "<br/>")
            'close the connection
            SqlConnection1.close()
        End Sub

    The SQLDataReader retrieves the data from the SQL Server and, using the Response.Write() method, writes it to the output HTML as shown in the next picture.

    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 3 hosted by Hostway
    Stay green...Green IT