Private Sub Page_Load(ByVal
sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
استفاده از يک متغير رشته ای بمنظور ذخيره سازی
Connectionstring'
Dim connectString As String =
"Provider=Microsoft.Jet.OLEDB.4.0;" &_
"Data Source=C:\MyDB\NWIND.MDB"
ايجاد يک شی OleDbConnection
و ارسال مقدار متغير ConnectionString '
Dim cn As OleDbConnection = New
OleDbConnection(connectString)
فعال نمودن Connection'
cn.Open()
استفاده از يک متغير بمنظور ذخيره عبارت
SQL'
Dim selectString As String = "SELECT
CustomerID, ContactName, Phone FROM Customers"
ايجاد يک شی OledbCommand'
در اين خط متغير عبارت
SQL و شی OleDbConnection
، ارسال می گردد '
Dim cmd As OleDbCommand = New
OleDbCommand(selectString, cn)
ارسال CommandText
به Connection و ايجاد يک OleDbDataReader
'
OleDbDataReader از نوع " فقط
بسمت جلو " خواهد بود'
Dim reader As OleDbDataReader =
cmd.ExecuteReader()
تنظيم عرض جدول '
DisplayTable.Width =
Unit.Percentage(90.0)
ايجاد يک سطر جديد برای اضافه نمودن عنوان جدول'
Dim tableHeading As TableRow = New
TableRow()
ايجاد و اضافه نمودن سلول های شامل ستون
Customer ID '
Dim customerIDHeading As
TableHeaderCell = New TableHeaderCell()
customerIDHeading.Text = "Customer ID"
customerIDHeading.HorizontalAlign = HorizontalAlign.Left
tableHeading.Cells.Add(customerIDHeading)
ايجاد و اضافه نمودن سلول های شامل ستون
Contact Name '
Dim contactNameHeading As
TableHeaderCell = New TableHeaderCell()
contactNameHeading.Text = "Contact Name"
contactNameHeading.HorizontalAlign = HorizontalAlign.Left
tableHeading.Cells.Add(contactNameHeading)
ايجاد و اضافه نمودن سلول های شامل ستون
Phone '
Dim phoneHeading As
TableHeaderCell = New TableHeaderCell()
phoneHeading.Text = "Phone"
phoneHeading.HorizontalAlign = HorizontalAlign.Left
tableHeading.Cells.Add(phoneHeading)
DisplayTable.Rows.Add(tableHeading)
تکرار در بين داده انتخابی نتايج و افزودن داده برای هر يک
از ستون های مورد نظر در جدول '
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)
DisplayTable.Rows.Add(detailsRow)
End While
بستن
Connection'
reader.Close()
cn.Close()
End Sub |