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()

Search

Feeds

RSS 2.0 feed All content
RSS 2.0 feed ajax
RSS 2.0 feed asp
RSS 2.0 feed aspnet
RSS 2.0 feed bicycle
RSS 2.0 feed copenhagen
RSS 2.0 feed databases
RSS 2.0 feed denmark
RSS 2.0 feed diy
RSS 2.0 feed dotnet
RSS 2.0 feed html
RSS 2.0 feed japan
RSS 2.0 feed javascript
RSS 2.0 feed modding
RSS 2.0 feed photography
RSS 2.0 feed utilities
RSS 2.0 feed vbscript