Posted By

blackf0rk on 03/12/09


Tagged

data loop reader through datareader


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

wilson0825
haoji


Loop through a DataReader


 / Published in: VB.NET
 

URL: http://codefinds.blogspot.com/2008/08/loop-through-datareader.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

Report this snippet  

You need to login to post a comment.