VB icon

How to use the DataReader (recordset)

Email
Submitted on: 1/1/2015 1:46:00 PM
By: DrDave (from psc cd)  
Level: Beginner
User Rating: By 3 Users
Compatibility: VB.NET, ASP.NET
Views: 768
 
     shows you how to use a SqlDataReader
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
//**************************************
// Name: How to use the DataReader (recordset)
// Description:shows you how to use a SqlDataReader
// By: DrDave (from psc cd)
//
// Inputs:sql statment
//
// Returns:SqlDataReader
//**************************************

How to use a System.Data.SqlClient.SqlDataReader
I have inserted ... at the begining of each line so that it is readable...
.
.
.' I Used the frmMain Load sub as an example.
.
.Private Sub frmMain_Load(ByVal Sender As Object, ByVal e As EventArgs) Handles MyBase.load
'Dim all the needed vars
....Dim DR As System.Data.SqlClient.SqlDataReader
....Dim strSQL As String = "SELECT * FROM Users"
....Dim SqlConn As new System.Data.SqlClient.SqlConnection("Persist Security Info=False;Data Source=localhost;Initial Catalog=MyDb;User ID=sa;Password=;")
'Open the Database
....SqlConn.open()
....Dim SqlComm As new System.Data.SqlClient.SqlCommand(strSQL, SqlConn)
'Execute the reader
....DR = SqlComm.ExecuteReader
'Loop through the records and add the "UserName" to a text box
....Do While DR.Read()
.......txtUserNames.Text &= VbCrLf & DR.Item("UserName")
....Loop
'Close everything
....DR.Close()
....SqlConn.Close()
....SqlComm.Dispose()
....SqlConn.Dispose()
'setting the values to nothing is not required but I like to do it anyway
....DR = Nothing
....SqlConn = Nothing
....SqlComm = Nothing
'and thats it
.End Sub


Other 1 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.