DATABASE

ASP.NET 4 in VB 2010 : ADO.NET Fundamentals (part 4) - Direct Data Access - The DataReader

9/12/2012 1:34:32 AM

5.3. The DataReader

Once you've defined your command, you need to decide how you want to use it. The simplest approach is to use a DataReader, which allows you to quickly retrieve all your results. The DataReader uses a live connection and should be used quickly and then closed. The DataReader is also extremely simple. It supports fast-forward-only read-only access to your results, which is generally all you need when retrieving information. Because of the DataReader's optimized nature, it provides better performance than the DataSet. It should always be your first choice for direct data access.

Before you can use a DataReader, make sure you've opened the connection:

myConnection.Open()

To create a DataReader, you use the ExecuteReader() method of the command object, as shown here:

' You don't need the new keyword, as the Command will create the DataReader.
Dim myReader As SqlDataReader
myReader = myCommand.ExecuteReader()

					  

These two lines of code define a variable for a DataReader and then create it by executing the command. Once you have the reader, you retrieve a single row at a time using the Read() method:

myReader.Read()    ' The first row in the result set is now available.

You can then access the values in the current row using the corresponding field names. The following example adds an item to a list box with the first name and last name for the current row:

lstNames.Items.Add(myReader("au_lname") & ", " & myReader("au_fname"))

To move to the next row, use the Read() method again. If this method returns True, a row of information has been successfully retrieved. If it returns False, you've attempted to read past the end of your result set. There is no way to move backward to a previous row.

As soon as you've finished reading all the results you need, close the DataReader and Connection:

myReader.Close()
myConnection.Close()

5.4. Putting It All Together

The next example demonstrates how you can use all the ADO.NET ingredients together to create a simple application that retrieves information from the Authors table. You can select an author record by last name using a drop-down list box, as shown in Figure 10.

Figure 10. Selecting an author

The full record is then retrieved and displayed in a simple label, as shown in Figure 11.

Figure 11. Author information

5.4.1. Filling the List Box

To start, the connection string is defined as a private variable for the page class and retrieved from the connection string:

Private connectionString As String = _
  WebConfigurationManager.ConnectionStrings("Pubs").ConnectionString

The list box is filled when the Page.Load event occurs. Because the list box is set to persist its view state information, this information needs to be retrieved only onceā€”the first time the page is displayed. It will be ignored on all postbacks.

Here's the code that fills the list from the database:

Protected Sub Page_Load(ByVal sender As Object, _
  ByVal e As EventArgs) Handles Me.Load

    If Not Page.IsPostBack Then
        FillAuthorList()
    End If
End Sub

Private Sub FillAuthorList()
    lstAuthor.Items.Clear()

    ' Define the Select statement.
    ' Three pieces of information are needed: the unique id
    ' and the first and last name.
    Dim selectSQL As String = "SELECT au_lname, au_fname, au_id FROM Authors"

					  

' Define the ADO.NET objects.
    Dim con As New SqlConnection(connectionString)
    Dim cmd As New SqlCommand(selectSQL, con)
    Dim reader As SqlDataReader

    ' Try to open database and read information.
    Try
        con.Open()
        reader = cmd.ExecuteReader()

        ' For each item, add the author name to the displayed
        ' list box text, and store the unique ID in the Value property.
        Do While reader.Read()
            Dim newItem As New ListItem()
            newItem.Text = reader("au_lname") & ", " & reader("au_fname")
            newItem.Value = reader("au_id").ToString()
            lstAuthor.Items.Add(newItem)
        Loop
        reader.Close()

    Catch err As Exception
        lblResults.Text = "Error reading list of names."
        lblResults.Text &= err.Message
    Finally
        con.Close()
    End Try
End Sub

					  

It uses the standard Connection, Command, and DataReader objects. The Connection is opened inside an error handling block so your page can handle any unexpected errors and provide information. A Finally block makes sure the connection is properly closed, even if an error occurs.

The actual code for reading the data uses a loop. With each pass, the Read() method is called to get another row of information. When the reader has read all the available information, this method will return False, the loop condition will evaluate to False, and the loop will end gracefully.

The unique ID (the value in the au_id field) is stored in the Value property of the list box for reference later. This is a crucial ingredient that is needed to allow the corresponding record to be queried again. If you tried to build a query using the author's name, you would need to worry about authors with the same name. You would also have the additional headache of invalid characters (such as the apostrophe in O'Leary) that would invalidate your SQL statement.

5.4.2. Retrieving the Record

The record is retrieved as soon as the user changes the selection in the list box. To make this possible, the AutoPostBack property of the list box is set to True so that its change events are detected automatically.

Protected Sub lstAuthor_SelectedIndexChanged(ByVal sender As Object, _
  ByVal e As EventArgs) Handles lstAuthor.SelectedIndexChanged

    ' Create a Select statement that searches for a record
    ' matching the specific author ID from the Value property.
    Dim selectSQL As String
    selectSQL = "SELECT * FROM Authors "
    selectSQL &= "WHERE au_id='" & lstAuthor.SelectedItem.Value & "'"

    ' Define the ADO.NET objects.
    Dim con As New SqlConnection(connectionString)
    Dim cmd As New SqlCommand(selectSQL, con)
    Dim reader As SqlDataReader

    ' Try to open database and read information.
    Try
        con.Open()
        reader = cmd.ExecuteReader()
        reader.Read()

        ' Build a string with the record information,
        ' and display that in a label.
        Dim sb As New StringBuilder()
        sb.Append("<b>")
        sb.Append(reader("au_lname"))
        sb.Append(", ")
        sb.Append(reader("au_fname"))
        sb.Append("</b><br />")
        sb.Append("Phone: ")
        sb.Append(reader("phone"))
        sb.Append("<br />")
        sb.Append("Address: ")
        sb.Append(reader("address"))
        sb.Append("<br />")
        sb.Append("City: ")
        sb.Append(reader("city"))
        sb.Append("<br />")
        sb.Append("State: ")
        sb.Append(reader("state"))
        sb.Append("<br />")
        lblResults.Text = sb.ToString()

        reader.Close()

    Catch err As Exception
        lblResults.Text = "Error getting author. "
        lblResults.Text &= err.Message
    Finally
        con.Close()
    End Try
End Sub

					  

The process is similar to the procedure used to retrieve the last names. There are only a couple of differences:

  • The code dynamically creates a SQL statement based on the selected item in the drop-down list box. It uses the Value property of the selected item, which stores the unique identifier. This is a common (and useful) technique.

  • Only one record is read. The code assumes that only one author has the matching au_id, which is reasonable since this field is unique.

NOTE

This example shows how ADO.NET works to retrieve a simple result set. Of course, ADO.NET also provides handy controls that go beyond this generic level and let you provide full-featured grids with sorting and editing. 

Other  
  •  SQL Server 2008 : Failover clustering - Installing a clustered SQL Server instance
  •  SQL Server 2008 : Failover clustering - Clustering topologies and failover rules
  •  SQL Server 2008 : Failover clustering - Clustering overview
  •  Exploiting SQL Injection : Stealing the Password Hashes
  •  Exploiting SQL Injection : Escalating Privileges
  •  Exploiting SQL Injection : Enumerating the Database Schema (part 3) - Oracle
  •  Exploiting SQL Injection : Enumerating the Database Schema (part 2) - MySQL
  •  Exploiting SQL Injection : Enumerating the Database Schema (part 1) - SQL Server
  •  SQL Server 2008 R2 : Creating and Managing Indexes - Creating Indexes (part 2) - Creating Indexes with SSMS
  •  SQL Server 2008 R2 : Creating and Managing Indexes - Creating Indexes (part 1) - Creating Indexes with T-SQL
  •  
    Video
    Top 10
    SG50 Ferrari F12berlinetta : Prancing Horse for Lion City's 50th
    The latest Audi TT : New angles for TT
    Era of million-dollar luxury cars
    Game Review : Hearthstone - Blackrock Mountain
    Game Review : Battlefield Hardline
    Google Chromecast
    Keyboards for Apple iPad Air 2 (part 3) - Logitech Ultrathin Keyboard Cover for iPad Air 2
    Keyboards for Apple iPad Air 2 (part 2) - Zagg Slim Book for iPad Air 2
    Keyboards for Apple iPad Air 2 (part 1) - Belkin Qode Ultimate Pro Keyboard Case for iPad Air 2
    Michael Kors Designs Stylish Tech Products for Women
    REVIEW
    - First look: Apple Watch

    - 3 Tips for Maintaining Your Cell Phone Battery (part 1)

    - 3 Tips for Maintaining Your Cell Phone Battery (part 2)
    Popular Tags
    Video Tutorail Microsoft Access Microsoft Excel Microsoft OneNote Microsoft PowerPoint Microsoft Project Microsoft Visio Microsoft Word Active Directory Exchange Server Sharepoint Sql Server Windows Server 2008 Windows Server 2012 Windows 7 Windows 8 Adobe Flash Professional Dreamweaver Adobe Illustrator Adobe Photoshop CorelDRAW X5 CorelDraw 10 windows Phone 7 windows Phone 8 Iphone