<%@ Import Namespace="System.Data"%>
<%@ Import Namespace="System.Data.OleDb"%>
<%@ Page Language="VB" %>
<script runat="server">
Sub Page_Load(sender As Object, e As EventArgs)
BindData()
End Sub
Sub BindData()
'*** DataSet From DataSet ***'
Dim ds As DataSet
ds = CreateDataSet()
Dim i As Integer
Dim dg As DataGrid
Dim lbl As Label
For i = 0 To ds.Tables.Count - 1
dg = New DataGrid
With dg
.DataSource = ds.Tables(i).DefaultView
.AutoGenerateColumns = True
.DataBind()
End With
lbl = New Label
lbl.Text = "<br><b>"&ds.Tables(i).ToString & "</b><br>"
Me.lblText.Controls.Add(lbl)
Me.lblText.Controls.Add(dg)
dg = Nothing
Next
End Sub
'*** DataSet ***'
Function CreateDataSet() As DataSet
Dim objConn As New System.Data.OleDb.OleDbConnection
Dim objCmd As New System.Data.OleDb.OleDbCommand
Dim dtAdapter As New System.Data.OleDb.OleDbDataAdapter
Dim ds As New DataSet
Dim i As Integer
Dim strConnString,strSQL As String
strConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="& _
Server.MapPath("database/mydatabase.mdb")&";Jet OLEDB:Database Password=;"
Dim TablesName(2) As String
TablesName(0) = "customer"
TablesName(1) = "audit"
TablesName(2) = "country"
For i = 0 To TablesName.GetUpperBound(0)
strSQL = "SELECT * FROM " & TablesName(i) & " "
objConn.ConnectionString = strConnString
With objCmd
.Connection = objConn
.CommandText = strSQL
.CommandType = CommandType.Text
End With
dtAdapter.SelectCommand = objCmd
dtAdapter.Fill(ds,TablesName(i))
Next
dtAdapter = Nothing
objConn.Close()
objConn = Nothing
Return ds '*** Return DataSet ***'
End Function
</script>
<html>
<head>
<title>ThaiCreate.Com ASP.NET - System.Data.OleDb</title>
</head>
<body>
<form id="form1" runat="server">
<asp:Label id="lblText" runat="Server"></asp:Label>
</form>
</body>
</html>
Imports System.Data
Imports System.Data.OleDb
Public Class ControlAdd
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'*** Create New Label ***'
Dim lblText As New Label
With lblText
.Text = "My Customer"
.Font.Size = "30"
End With
Me.pnlMain1.Controls.Add(lblText)
'*** Create DataGrid ***'
Dim myDataGrid As New DataGrid
With myDataGrid
.AutoGenerateColumns = True
.DataSource = DataSource()
.DataBind()
End With
Me.pnlMain2.Controls.Add(myDataGrid)
End Sub
Protected Function DataSource()
Dim objConn As New System.Data.OleDb.OleDbConnection
Dim dtAdapter As System.Data.OleDb.OleDbDataAdapter
Dim dt As New DataTable
Dim strConnString As String
strConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("database/mydatabase.mdb") & ";"
objConn = New System.Data.OleDb.OleDbConnection(strConnString)
objConn.Open()
Dim strSQL As String
strSQL = "SELECT * FROM customer"
dtAdapter = New System.Data.OleDb.OleDbDataAdapter(strSQL, objConn)
dtAdapter.Fill(dt)
dtAdapter = Nothing
objConn.Close()
objConn = Nothing
Return dt '*** Return DataTable ***'
End Function
End Class