Loop through and list records from a database with ASP.Net

A basic example showing how to loop through records in a database from a C# or VB.Net web application (using System.Data.OleDb).

C#

string strConn = @"PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=d:\test.mdb;";
string strSQL = "SELECT uID, fullName FROM persons WHERE uID < 100";
OleDbConnection objConnection = new OleDbConnection(strConn);
OleDbCommand objCommand = new OleDbCommand(strSQL,objConnection);
objConnection.Open();
OleDbDataReader objReader = objCommand.ExecuteReader();
while (objReader.Read())
{
  Response.Write(objReader.GetInt32(0) + ", ");
  Response.Write(objReader.GetString(1) + "<br>");
}
objReader.Close();
objConnection.Close();

VB.Net

Dim strConn As String = "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=d:\test.mdb;"
Dim strSQL As String = "SELECT uID, fullName FROM persons WHERE uID < 100"
Dim objConnection As New OleDbConnection(strConn)
Dim objCommand As New OleDbCommand(strSQL, objConnection)
objConnection.Open()
Dim objReader As OleDbDataReader = objCommand.ExecuteReader()
While objReader.Read()
  Response.Write(objReader.GetInt32(0) & ", ")
  Response.Write(objReader.GetString(1) & "<br>")
End While
objReader.Close()
objConnection.Close()