For a Microsoft Visual C# .NET version of this article, see 308100 .
This article refers to the following Microsoft .NET Framework Class Library namespaces:
- System.Data
- System.Data.OleDb
Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.
Imports System.Data.OleDb
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Use a string variable to hold the ConnectionString.
Dim connectString As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=C:\File Databases\NWIND.MDB"
'Create an OleDbConnection object,
'and then pass in the ConnectionString to the constructor.
Dim cn As OleDbConnection = New OleDbConnection(connectString)
'Open the connection.
cn.Open()
'Use a variable to hold the SQL statement.
Dim selectString As String = "SELECT CustomerID, ContactName, Phone FROM Customers"
'Create an OleDbCommand object.
'Notice that this line passes in the SQL statement and the OleDbConnection object.
Dim cmd As OleDbCommand = New OleDbCommand(selectString, cn)
'Send the CommandText to the connection, and then build an OleDbDataReader.
'Note: The OleDbDataReader is forward-only.
Dim reader As OleDbDataReader = cmd.ExecuteReader()
'Set the table width.
DisplayTable.Width = Unit.Percentage(90.0)
'Create a new row for adding a table heading
Dim tableHeading As TableRow = New TableRow()
'Create and add the cells that contain the Customer ID column heading text.
Dim customerIDHeading As TableHeaderCell = New TableHeaderCell()
customerIDHeading.Text = "Customer ID"
customerIDHeading.HorizontalAlign = HorizontalAlign.Left
tableHeading.Cells.Add(customerIDHeading)
'Create and add the cells that contain the Contact Name column heading text.
Dim contactNameHeading As TableHeaderCell = New TableHeaderCell()
contactNameHeading.Text = "Contact Name"
contactNameHeading.HorizontalAlign = HorizontalAlign.Left
tableHeading.Cells.Add(contactNameHeading)
'Create and add the cells that contain the Phone column heading text.
Dim phoneHeading As TableHeaderCell = New TableHeaderCell()
phoneHeading.Text = "Phone"
phoneHeading.HorizontalAlign = HorizontalAlign.Left
tableHeading.Cells.Add(phoneHeading)
DisplayTable.Rows.Add(tableHeading)
'Loop through the resultant data selection and add the data value
'for each respective column in the table.
While(reader.Read())
Dim detailsRow As TableRow = New TableRow()
Dim customerIDCell As TableCell = New TableCell()
customerIDCell.Text = reader("CustomerID").ToString()
detailsRow.Cells.Add(customerIDCell)
Dim contactNameCell As TableCell = New TableCell()
contactNameCell.Text = reader("ContactName").ToString()
detailsRow.Cells.Add(contactNameCell)
Dim phoneCell As TableCell = New TableCell()
phoneCell.Text = reader("Phone").ToString()
detailsRow.Cells.Add(phoneCell)
'Add the new row to the table.
DisplayTable.Rows.Add(detailsRow)
End While
'Close the reader and the related connection.
reader.Close()
cn.Close()
End Sub
Keywords: kbdsupport, kbhowtomaster, kbsystemdata, kb