◎以上程式範例AdoNet01.aspx,如在頁框下不能操作,請開新視窗操作
◎如果有問題歡迎您提出,dnowba很需要有人和我一起討論
'===自已寫的===== Imports System.Data '================ Partial Class AdoNet01 Inherits System.Web.UI.Page Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Me.Label1.Text = "ADO.NET範例(AdoNet01.aspx):加入DataSet(資料集)、DataTable(資料表)" Me.Button1.Text = "建立名稱為POS的DataSet(資料集),並塞入名為Product、Customer...的DataTable(資料表)" Me.Button2.Text = "建立名稱為POS的DataSet(資料集),並塞入預設名稱的DataTable(資料表)" Me.Button3.Text = "DataTable(資料表)中,指定的TableName資料表名稱必須是唯一的" Me.Button4.Text = "簡潔的DataSet(資料集)、DataTable(資料表)寫法" End Sub Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click '===== 建立DataSet資料集的方式如下 ===== '第一句是宣告,第二句是用new來把物件實體化,如果只有第一行, '那麼就是未使用的區域變數,句子中的二個DataSet1不一樣,前者是變數,後者是名稱。 'Dim DataSet1 As DataSet 'DataSet1 = New DataSet("DataSet1") '微軟建議用一行的方式宣告並實體化,而且命名的規則以增加程式可讀性為原則,以下是修改後 '變數字首用小寫,若無關全區變數,名稱亂取是還好, '但像物件導向的.NET,我建議用全名稱的縮寫,例如DataSet用ds & DataSetName的方式處理較易辨別 Dim dsPOS As New DataSet("POS") '===== 建立DataTable資料表 ===== '先建立建立DataTable物件 Dim dtProduct As New DataTable("Product") Dim dtCustomer As New DataTable("Customer") Dim dtOrder As New DataTable("Order") '再把資料表塞到資料集中 dsPOS.Tables.Add(dtCustomer) dsPOS.Tables.Add(dtProduct) dsPOS.Tables.Add(dtOrder) '以下只是要列出剛增加的資料表名稱 For i As Integer = 0 To dsPOS.Tables.Count - 1 Dim liPOS As String = dsPOS.Tables(i).TableName Response.Write("資料表:" & liPOS & "建立成功<P>") Next End Sub Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click Dim dsPOS As New DataSet("POS") '注意如果在定義變數時沒有給它「指定名稱」(就是null或空字串),那麼在產生資料表的時候 '系統就會用預設的名稱,如下是範例,建議還是取個有意義的名稱會比較可讀 Dim dtProduct1 As New DataTable() Dim dtCustomer1 As New DataTable() Dim dtOrder1 As New DataTable() dsPOS.Tables.Add(dtCustomer1) dsPOS.Tables.Add(dtProduct1) dsPOS.Tables.Add(dtOrder1) '以下只是要列出剛增加的資料表名稱 For i As Integer = 0 To dsPOS.Tables.Count - 1 Dim liPOS As String = dsPOS.Tables(i).TableName Response.Write("資料表:" & liPOS & "建立成功<P>") Next End Sub Protected Sub Button3_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button3.Click Dim dsPOS As New DataSet("POS") '這裡故意把相同指定名稱的DataTable塞到DataSet裡,我們用Try..Catch..End try陳述式來抓出錯誤例外訊息 Try Dim dtProduct1 As New DataTable() Dim dtCustomer1 As New DataTable() Dim dtOrder1 As New DataTable() dsPOS.Tables.Add(dtCustomer1) dsPOS.Tables.Add(dtCustomer1) dsPOS.Tables.Add(dtOrder1) Catch ex As Exception 'Response.Write(ex) Dim customErr As String customErr = "程式出錯,不允許在一DataSet建立相同名稱的DataTable,以下是.NET的偵錯訊息:<BR>" Response.Write(customErr & ex.ToString) End Try End Sub Protected Sub Button4_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button4.Click Dim dsPOS As New DataSet("POS") Dim dtProduct As DataTable = dsPOS.Tables.Add("Product") Dim dtCustomer As DataTable = dsPOS.Tables.Add("Customer") Dim dtOrder As DataTable = dsPOS.Tables.Add("Order") For i As Integer = 0 To dsPOS.Tables.Count - 1 Dim liPOS As String = dsPOS.Tables(i).TableName Response.Write("資料表:" & liPOS & "建立成功<P>") Next End Sub End Class
沒有留言:
張貼留言