Loop through a DataReader


/ Published in: VB.NET
Save to your folder(s)



Copy this code and paste it in your HTML
  1. Dim objDR As SqlClient.SqlDataReader
  2. Dim objCommand As SqlClient.SqlCommand
  3. Dim ConnectionString As String = "YOUR CONNECTION STRING HERE"
  4. Dim objConnection As SqlClient.SqlConnection
  5. Dim ssql As String
  6.  
  7. objConnection = New SqlClient.SqlConnection(ConnectionString)
  8. ssql = "SELECT * FROM someTable"
  9.  
  10. If objConnection.State <> ConnectionState.Open Then
  11. objConnection.Open()
  12. End If
  13. objCommand = New SqlClient.SqlCommand(ssql, objConnection)
  14. objDR = objCommand.ExecuteReader(CommandBehavior.CloseConnection)
  15. objCommand = Nothing
  16.  
  17. If objDR.HasRows Then
  18. While objDR.Read()
  19. ' do your code here
  20. ' the following gives access
  21. ' to the table's field:
  22. ' objDR.Item("someField")
  23. End While
  24. End If
  25. objDR.Close()
  26. objDR = Nothing

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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.