Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Community Wiki

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Navigation

Google Ads

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.7/5 (6 votes cast)