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 |