Login or Sign Up to become a member!

EXPERTS, INFORMATION, IDEAS & KNOWLEDGE

Social bookmarker Add this

ASP.NET: Loop through data using a DataReader

From Wiki

Jump to: navigation, search


Summary: An example that uses the Northwind Database to loop through data using a DataReader

  1. ' Declarations  
  2. Dim MyConnection As System.Data.SQLClient.SqlConnection
  3. Dim MyCommand As System.Data.SQLClient.SqlCommand
  4. Dim MyDataReader As System.Data.SQLClient.SqlDataReader
  5.  
  6. ' Open the connection and execute a SQL Statement  
  7. MyConnection = New System.Data.SQLClient.SqlConnection("server=127.0.0.1; initial catalog=Northwind;uid=USERNAME;pwd=PASSWORD")  
  8. MyConnection.Open()  
  9. MyCommand = New System.Data.SQLClient.SqlCommand  
  10. MyCommand.Connection = MyConnection  
  11. MyCommand.CommandText = "SELECT ProductName FROM products"  
  12. MyDataReader = MyCommand.ExecuteReader()  
  13.  
  14. ' Loop through the DataReader and write out each entry  
  15. While MyDataReader.Read  
  16.     Response.Write("Name: " & MyDataReader.Item("ProductName"))  
  17. End While  
  18.  
  19. ' Clean Up  
  20. MyDataReader.Close()  
  21. MyDataReader = Nothing  
  22. MyCommand = Nothing  
  23. MyConnection.Close()  
  24. MyConnection = Nothing


This Hack is part of the ASP.NET Hacks collection

449 Rating: 2.8/5 (5 votes cast)