0% found this document useful (0 votes)
125 views4 pages

Understanding DataReaders in SQL

A DataReader is an object that provides read-only, forward-only access to data in an efficient manner. It requires an active connection and allows reading data from a database one record at a time using the Read method. Sample code is provided to retrieve, insert, delete, and update data using a DataReader with a SQL connection.

Uploaded by

Surendra Lilhore
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
125 views4 pages

Understanding DataReaders in SQL

A DataReader is an object that provides read-only, forward-only access to data in an efficient manner. It requires an active connection and allows reading data from a database one record at a time using the Read method. Sample code is provided to retrieve, insert, delete, and update data using a DataReader with a SQL connection.

Uploaded by

Surendra Lilhore
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd

DataReaders

A DataReader is a lightweight object that provides read-only, forward-only data in a


very fast and efficient way. Using a DataReader is efficient than using a DataAdapter
but it is limited. Data access with DataReader is
read-only, meaning, we cannot make any changes (update) to data and forward-
only, which means we cannot go back to the previous record which was accessed. A
DataReader requires the exclusive use of an active connection for the entire time it is
in existence. We instantiate a DataReader by making a call to a Command
object's ExecuteReader command. When the DataReader is first returned it is
positioned before the first record of the result set. To make the first record available
we need to call the Read method. If a record is available, the Read method moves
the DataReader to next record and returns True. If a record is not available the Read
method returns False. We use a While Loop to iterate through the records with the
Read method.

Sample Code

Code to Retrieve Data using Select Command

The following code displays data from Discounts table in Pubs sample database.

Imports [Link]
Public Class Form1 Inherits [Link]
Dim myConnection As SqlConnection
Dim myCommand As SqlCommand
Dim dr As New SqlDataReader()
'declaring the objects

Private Sub Form1_Load(ByVal sender As [Link], ByVal e As


[Link])_
Handles [Link]
myConnection = New
SqlConnection("server=localhost;uid=sa;pwd=;database=pubs")
'establishing connection. you need to provide password for sql
server
Try
[Link]()
'opening the connection
myCommand = New SqlCommand("Select * from discounts",
myConnection)
'executing the command and assigning it to connection
dr = [Link]()
While [Link]()
'reading from the datareader
[Link]("discounttype" & dr(0).ToString())
[Link]("stor_id" & dr(1).ToString())
[Link]("lowqty" & dr(2).ToString())
[Link]("highqty" & dr(3).ToString())
[Link]("discount" & dr(4).ToString())
'displaying the data from the table
End While
[Link]()
[Link]()
Catch e As Exception
End Try
End Sub

End Class

The above code displays records from discounts table in MessageBoxes.

Retrieving records with a Console Application

Imports [Link]
Imports [Link]
Module Module1

Dim myConnection As SqlConnection


Dim myCommand As SqlCommand
Dim dr As SqlDataReader

Sub Main()
Try
myConnection = New
SqlConnection("server=localhost;uid=sa;pwd=;database=pubs")
'you need to provide password for sql server
[Link]()
myCommand = New SqlCommand("Select * from discounts",
myConnection)
dr = [Link]
Do
While [Link]()
WriteLine(dr(0))
WriteLine(dr(1))
WriteLine(dr(2))
WriteLine(dr(3))
WriteLine(dr(4))
' writing to console
End While
Loop While [Link]()
Catch
End Try
[Link]()
[Link]()
End Sub

End Module
Using DataReaders, SQL Server
Inserting Records
The following code inserts a Record into the Jobs table
in Pubs sample database. Drag a button onto the form and place the
following code.
Imports [Link]
Public Class Form2 Inherits [Link]
Dim myConnection As SqlConnection
Dim myCommand As SqlCommand
Dim ra as Integer
'integer holds the number of records inserted
Private Sub Form2_Load(ByVal sender As [Link], ByVal
e_
As [Link]) Handles [Link]
End Sub
Private Sub Button1_Click(ByVal sender As [Link], ByVal
e_
As [Link]) Handles [Link]
myConnection = New
SqlConnection("server=localhost;uid=sa;pwd=;database=pubs")
'you need to provide password for sql server
[Link]()
myCommand = New SqlCommand("Insert into Jobs values 12,'IT
Manager',100,300,_
myConnection)
ra=[Link]()
[Link]("New Row Inserted" & ra)
[Link]()
End Sub
End Class
Deleting a Record
We will use Authors table in Pubs sample database to work with this
code. Drag a button onto the form and place the following code.
Imports [Link]
Public Class Form3 Inherits [Link]
Dim myConnection As SqlConnection
Dim myCommand As SqlCommand
Dim ra as Integer
Private Sub Form3_Load(ByVal sender As [Link], ByVal
e_
As [Link]) Handles [Link]
End Sub
Private Sub Button1_Click(ByVal sender As [Link], ByVal
e_
As [Link]) Handles [Link]
myConnection = New
SqlConnection("server=localhost;uid=sa;pwd=;database=pubs")
'you need to provide password for sql server
[Link]()
myCommand = New SqlCommand("Delete from Authors where
city='Oakland'",_
myConnection)
'since no value is returned we use ExecuteNonQuery
ra=[Link]()
[Link]("Records affected" & ra)
[Link]()
End Sub
End Class
Updating Records
We will update a row in Authors table. Drag a button onto the
form and place the following code.
Imports [Link]
Public Class Form4 Inherits [Link]
Dim myConnection As SqlConnection
Dim myCommand As SqlCommand
Dim ra as Integer
Private Sub Form4_Load(ByVal sender As [Link], ByVal
e_
As [Link]) Handles [Link]
End Sub
Private Sub Button1_Click(ByVal sender As [Link], ByVal
e_
As [Link]) Handles [Link]
myConnection = New
SqlConnection("server=localhost;uid=sa;pwd=;database=pubs")
'you need to provide password for sql server
[Link]()
myCommand = New SqlCommand("Update Authors Set
city='Oakland' where city=_
'San Jose' ",myConnection)
ra=[Link]()
[Link]("Records affected" & ra)
[Link]()
End Sub
End Class

You might also like