Return to Snippet

Revision: 12396
at March 12, 2009 17:22 by blackf0rk


Initial Code
Dim objDR As SqlClient.SqlDataReader
Dim objCommand As SqlClient.SqlCommand
Dim ConnectionString As String = "YOUR CONNECTION STRING HERE"
Dim objConnection As SqlClient.SqlConnection
Dim ssql As String

objConnection = New SqlClient.SqlConnection(ConnectionString)
ssql = "SELECT * FROM someTable"

If objConnection.State <> ConnectionState.Open Then
   objConnection.Open()
End If
objCommand = New SqlClient.SqlCommand(ssql, objConnection)
objDR = objCommand.ExecuteReader(CommandBehavior.CloseConnection)
objCommand = Nothing

If objDR.HasRows Then
   While objDR.Read()
      ' do your code here
      ' the following gives access
      ' to the table's field:
      ' objDR.Item("someField")
   End While
End If
objDR.Close()
objDR = Nothing

Initial URL
http://codefinds.blogspot.com/2008/08/loop-through-datareader.html

Initial Description

                                

Initial Title
Loop through a DataReader

Initial Tags
data

Initial Language
VB.NET