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.
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



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