ASP.NET System.Data.OracleClient - DataTable() Part 2
ASP.NET System.Data.OracleClient - DataTable() Part 2 การเขียน ASP.NET ใช้งาน NameSpace System.Data.OracleClient กับ DataTable() ในการวนลูปข้อมูลเพื่อสร้างเป็นตารางด้วย Table,TableRow,TableCell
<%@ Import Namespace="System.Data"%>
<%@ Import Namespace="System.Data.OracleClient"%>
<%@ Page Language="VB" %>
<script runat="server">
Sub Page_Load(sender As Object, e As EventArgs)
BindTable()
End Sub
'*** Bind To Table ***'
Sub BindTable()
'*** Test DataTable Structure ***'
'********************************'
Dim dt As DataTable
Dim i As Integer
dt = CreateDataTable()
'*** Create Table **'
Dim Tb As New Table
With Tb
'.BorderColor = Drawing.Color.Black
.BorderWidth = 1
End With
'*** Rows ***'
Dim Tr As TableRow
'*** Column ***'
Dim Tc As TableCell
'*** Create Header ***'
'*** New Rows ***'
Tr = New TableRow()
'*** New Cell (Header CustomerID) ***'
Tc = New TableCell()
'Tc.BorderColor = Drawing.Color.Black
Tc.BorderWidth = 1
Dim lblHeadCustomerID As New Label
With lblHeadCustomerID
.Text = "CustomerID"
.Font.Bold = True
End With
Tc.Controls.Add(lblHeadCustomerID) '*** Add lblHeadCustomerID To Cell ***'
Tr.Cells.Add(Tc) '*** Add Cell To Rows ***'
'*** New Cell (Header Name) ***'
Tc = New TableCell()
'Tc.BorderColor = Drawing.Color.Black
Tc.BorderWidth = 1
Dim lblHeadName As New Label
With lblHeadName
.Text = "Name"
.Font.Bold = True
End With
Tc.Controls.Add(lblHeadName) '*** Add lblHeadName To Cell ***'
Tr.Cells.Add(Tc) '*** Add Cell To Rows ***'
'*** New Cell (Header Email) ***'
Tc = New TableCell()
'Tc.BorderColor = Drawing.Color.Black
Tc.BorderWidth = 1
Dim lblHeadEmail As New Label
With lblHeadEmail
.Text = "Email"
.Font.Bold = True
End With
Tc.Controls.Add(lblHeadEmail) '*** Add lblHeadEmail To Cell ***'
Tr.Cells.Add(Tc) '*** Add Cell To Rows ***'
'*** New Cell (Header CountryCode) ***'
Tc = New TableCell()
'Tc.BorderColor = Drawing.Color.Black
Tc.BorderWidth = 1
Dim lblHeadCountryCode As New Label
With lblHeadCountryCode
.Text = "CountryCode"
.Font.Bold = True
End With
Tc.Controls.Add(lblHeadCountryCode) '*** Add lblHeadCountryCode To Cell ***'
Tr.Cells.Add(Tc) '*** Add Cell To Rows ***'
'*** New Cell (Header Budget) ***'
Tc = New TableCell()
'Tc.BorderColor = Drawing.Color.Black
Tc.BorderWidth = 1
Dim lblHeadBudget As New Label
With lblHeadBudget
.Text = "Budget"
.Font.Bold = True
End With
Tc.Controls.Add(lblHeadBudget) '*** Add lblHeadBudget To Cell ***'
Tr.Cells.Add(Tc) '*** Add Cell To Rows ***'
'*** New Cell (Header Used) ***'
Tc = New TableCell()
'Tc.BorderColor = Drawing.Color.Black
Tc.BorderWidth = 1
Dim lblHeadUsed As New Label
With lblHeadUsed
.Text = "Used"
.Font.Bold = True
End With
Tc.Controls.Add(lblHeadUsed) '*** Add lblHeadUsed To Cell ***'
Tr.Cells.Add(Tc) '*** Add Cell To Rows ***'
Tb.Controls.Add(Tr)
'*** End Header ***'
'*************** Input DataTable To TableRows ***************'
For i = 0 To dt.Rows.Count - 1
Tr = New TableRow()
'*** New Cell (CustomerID) ***'
Tc = New TableCell()
'Tc.BorderColor = Drawing.Color.Black
Tc.HorizontalAlign = HorizontalAlign.Center
Tc.BorderWidth = 1
Dim lblCustomerID As New Label
With lblCustomerID
.Text = dt.Rows(i)("CustomerID")
End With
Tc.Controls.Add(lblCustomerID) '*** Add lblCustomerID To Cell ***'
Tr.Cells.Add(Tc) '*** Add Cell To Rows ***'
'*** New Cell (Name) ***'
Tc = New TableCell()
'Tc.BorderColor = Drawing.Color.Black
Tc.BorderWidth = 1
Dim lblName As New Label
With lblName
.Text = dt.Rows(i)("Name")
End With
Tc.Controls.Add(lblName) '*** Add lblName To Cell ***'
Tr.Cells.Add(Tc) '*** Add Cell To Rows ***'
'*** New Cell (Email) ***'
Tc = New TableCell()
'Tc.BorderColor = Drawing.Color.Black
Tc.BorderWidth = 1
Dim lblEmail As New Label
With lblEmail
.Text = dt.Rows(i)("Email")
End With
Tc.Controls.Add(lblEmail) '*** Add lblEmail To Cell ***'
Tr.Cells.Add(Tc) '*** Add Cell To Rows ***'
'*** New Cell (CountryCode) ***'
Tc = New TableCell()
'Tc.BorderColor = Drawing.Color.Black
Tc.BorderWidth = 1
Dim lblCountryCode As New Label
With lblCountryCode
.Text = dt.Rows(i)("CountryCode")
End With
Tc.Controls.Add(lblCountryCode) '*** Add lblCountryCode To Cell ***'
Tr.Cells.Add(Tc) '*** Add Cell To Rows ***'
'*** New Cell (Budget) ***'
Tc = New TableCell()
'Tc.BorderColor = Drawing.Color.Black
Tc.BorderWidth = 1
Dim lblBudget As New Label
With lblBudget
.Text = dt.Rows(i)("Budget")
End With
Tc.Controls.Add(lblBudget) '*** Add lblBudget To Cell ***'
Tr.Cells.Add(Tc) '*** Add Cell To Rows ***'
'*** New Cell (Used) ***'
Tc = New TableCell()
'Tc.BorderColor = Drawing.Color.Black
Tc.BorderWidth = 1
Dim lblUsed As New Label
With lblUsed
.Text = dt.Rows(i)("Used")
End With
Tc.Controls.Add(lblUsed) '*** Add lblUsed To Cell ***'
Tr.Cells.Add(Tc) '*** Add Cell To Rows ***'
Tb.Controls.Add(Tr)
Next
'************ End Input DataTable To TableRows *************'
Me.lblTable.Controls.Add(Tb)
End Sub
'*** DataTable ***'
Function CreateDataTable() As DataTable
Dim objConn As New System.Data.OracleClient.OracleConnection
Dim dtAdapter As System.Data.OracleClient.OracleDataAdapter
Dim dt As New DataTable
Dim strConnString As String
strConnString = "Data Source=TCDB;User Id=myuser;Password=mypassword;"
objConn = New System.Data.OracleClient.OracleConnection(strConnString)
objConn.Open()
Dim strSQL As String
strSQL = "SELECT * FROM customer"
dtAdapter = New System.Data.OracleClient.OracleDataAdapter(strSQL, objConn)
dtAdapter.Fill(dt)
dtAdapter = Nothing
objConn.Close()
objConn = Nothing
Return dt '*** Return DataTable ***'
End Function
</script>
<html>
<head>
<title>ThaiCreate.Com ASP.NET - System.Data.OracleClient</title>
</head>
<body>
<form id="form1" runat="server">
<asp:Label id="lblTable" runat="server"></asp:Label>
</form>
</body>
</html>