ASP.NET: Loop through data using a DataReader
From Wiki
Summary: An example that uses the Northwind Database to loop through data using a DataReader
- ' Declarations
- Dim MyConnection As System.Data.SQLClient.SqlConnection
- Dim MyCommand As System.Data.SQLClient.SqlCommand
- Dim MyDataReader As System.Data.SQLClient.SqlDataReader
- ' Open the connection and execute a SQL Statement
- MyConnection = New System.Data.SQLClient.SqlConnection("server=127.0.0.1; initial catalog=Northwind;uid=USERNAME;pwd=PASSWORD")
- MyConnection.Open()
- MyCommand = New System.Data.SQLClient.SqlCommand
- MyCommand.Connection = MyConnection
- MyCommand.CommandText = "SELECT ProductName FROM products"
- MyDataReader = MyCommand.ExecuteReader()
- ' Loop through the DataReader and write out each entry
- While MyDataReader.Read
- Response.Write("Name: " & MyDataReader.Item("ProductName"))
- End While
- ' Clean Up
- MyDataReader.Close()
- MyDataReader = Nothing
- MyCommand = Nothing
- MyConnection.Close()
- MyConnection = Nothing
This Hack is part of the ASP.NET Hacks collection


