Retrieve Data Using Data Readers

This tutorial is part of Database Programming Made Easy Series. Retrieve Data Using Data Readers Data reader is like an ado forward-only/read-only client side record set. Data reader is best suited if you only need to retrieve data, that is, you don't want to save the data back to the database. To retrieve and save the data you need a dataset which will be discuss in the next topic. When retrieving data using data reader you take advantage of loading your data faster than dataset. Data reader will loop through your recordset by means of SqlDataReader for SQL Server or OleDbDataReader for MS Access. Now what’s the use of a data reader if it cannot save data back to the database? This simply means that you need only to read the data like determining the number of record in a table or getting a value from one or more field. Say for example. If you want to know how many Customers are living in Seattle you would access it by using this SQL statement: SELECT * FROM Customers WHERE City = 'Seattle' And then execute it using a data reader. In this way the record set will return faster compare to dataset. Let’s try it out using the connection we’ve need in the previous topic. The following code will retrieve data in a Customer’s table where City is equal to Seattle. 1. Data Reader using MS Access
  1.         'Set up connection string
  2.         Dim cnString As String
  3.  
  4.         cnString = "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source=..\data\Northwind.mdb"
  5.  
  6.         'create command (with both text and connection)
  7.         Dim sqlQRY As String = "SELECT * FROM Customers WHERE City = 'Seattle'"
  8.  
  9.         'Create connection
  10.         Dim conn As OleDbConnection = New OleDbConnection(cnString)
  11.  
  12.         Try
  13.             ' Open connection
  14.             conn.Open()
  15.  
  16.             Dim cmd As OleDbCommand = New OleDbCommand(sqlQRY, conn)
  17.  
  18.             'create data reader
  19.             Dim rdr As OleDbDataReader = cmd.ExecuteReader
  20.  
  21.             While (rdr.Read)
  22.                 txtStatus.Text = txtStatus.Text & rdr("CompanyName").ToString()
  23.             End While
  24.  
  25.         Catch ex As SqlException
  26.             txtStatus.Text = "Error: " & ex.ToString & vbCrLf
  27.         Finally
  28.             ' Close connection
  29.             conn.Close()
  30.             txtStatus.Text = txtStatus.Text & vbCrLf & "Connection closed."
  31.         End Try
2. Data Reader using Microsoft SQL Server
  1.         'Set up connection string
  2.         Dim cnString As String
  3.  
  4.         cnString = "Data Source=localhost;Integrated Security=True;database=northwind"
  5.  
  6.         'create command (with both text and connection)
  7.         Dim sqlQRY As String = "SELECT * FROM Customers WHERE City = 'Seattle'"
  8.  
  9.         'Create connection
  10.         Dim conn As SqlConnection = New SqlConnection(cnString)
  11.  
  12.         Try
  13.             ' Open connection
  14.             conn.Open()
  15.  
  16.             Dim cmd As SqlCommand = New SqlCommand(sqlQRY, conn)
  17.  
  18.             'create data reader
  19.             Dim rdr As SqlDataReader = cmd.ExecuteReader
  20.  
  21.             While (rdr.Read)
  22.                 txtStatus.Text = txtStatus.Text & rdr("CompanyName").ToString()
  23.             End While
  24.  
  25.         Catch ex As SqlException
  26.             txtStatus.Text = "Error: " & ex.ToString & vbCrLf
  27.         Finally
  28.             ' Close connection
  29.             conn.Close()
  30.             txtStatus.Text = txtStatus.Text & vbCrLf & "Connection closed."
  31.         End Try
  32.     End Sub
To simplify this tutorial I remove some line of code which is not actually part of the discussion. You will, however, see it in the source code file. By examining the code above the only difference between MS Access and SQL Server is by connecting to a database, creating a command and executing a data reader as shown below.

Microsoft Access

Microsoft SQL Server

OleDbConnection

SqlConnection

OleDbCommand

SqlCommand

OleDbDataReader

SqlDataReader

The important part here is on how you can retrieve the data. This can be done using the code below:
  1. While (rdr.Read)
  2.      txtStatus.Text = txtStatus.Text & rdr("CompanyName").ToString()
  3. End While
Previous: How to Modify Data Next: How to Retrieve Data Using Datasets and Data Adapters

Comments

You can used also this command txtStatus.Text = txtStatus.Text & rdr!CompanyName for more simplistic approach... :)

In reply to by Anonymous (not verified)

Yes. This is also acceptable. Thanks

The code was very useful, I have just started learning the language and was experiencing some amount of difficulty retreiving the data from the database for enquiry, the code really helped.

Hello sir... ur site is really good and very helpfull....but i wish it cud be for C#.NET ,am not into Visual Basic actually...can u plss help me out...i mean u dnt hav d same wid C# codes....??? Thanks....

Hello sir... ur site is really good and veryhelpfull,,,but i am not into VIsual Basic actually,,can u please provide the same site wid C#.NET codes......i wud b vry gratefull to u...!!!

very useful

Thnx sir.. BUT WITHOUT GIVING, SELECT * FROM Customers WHERE City = 'Seattle' for the WHERE condition i want to retrieve under any City name given from the CityTable.. sir how to do that??? Thank you

hi i just wanna ask i have a thesis about monitoring and assets management system i just wanna know ho will i connect two datagrid to each other, and in monitoring how will i do it using RFID?thanks in advance!

Line 25 in the example for Access connection is wrong. It should be an OleDbException. Otherwise it doesn't catch the errors. Just saying! Otherwise nice code! Cheers!

Add new comment