1 / 152

ASP.NET 網頁製作教本 – 從基本語法學起

ASP.NET 網頁製作教本 – 從基本語法學起. 第9章 ADO.NET 與資料庫存取 ( 一 ). 9-1 ADO.NET 物件概述. ADO.NET 物件概述. ADO.NET 常用物件概述 (1). ADO.NET 常用物件概述 (2). 資料庫存取的過程. 命名空間的引用 (1). ADO.NET 物件中, DataSet 物件附屬於 System.Data 命名空間,若要在程式之中使用它,須先匯入 System.Data 命名空間,標示如下:.

corine
Télécharger la présentation

ASP.NET 網頁製作教本 – 從基本語法學起

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. ASP.NET 網頁製作教本 – 從基本語法學起 第9章 ADO.NET 與資料庫存取(一)

  2. 9-1 ADO.NET 物件概述

  3. ADO.NET 物件概述

  4. ADO.NET 常用物件概述 (1)

  5. ADO.NET常用物件概述 (2)

  6. 資料庫存取的過程

  7. 命名空間的引用 (1) • ADO.NET 物件中,DataSet 物件附屬於System.Data 命名空間,若要在程式之中使用它,須先匯入 System.Data 命名空間,標示如下: <%@ Import Namespace="System.Data" %>

  8. 命名空間的引用 (2) • 而Connection、Command、DataAdapter 及 DataReader 物件則附屬於System.Data.OleDb 或 System.Data.SqlClient,若要在程式中使用它們,須先匯入 System.Data.OleDb 或 System.Data.SqlClient 命名空間,標示如下: <%@ Import Namespace="System.Data.OleDb" %><%@ Import Namespace="System.Data.SqlClient" %>

  9. SqlClient 與 OleDb 的區別 • System.Data.OleDb 是通用的資料庫存取物件,可用於存取 Access、SQL Server、dBase、Excel…等格式的資料庫,而System.Data.SqlClient 則是存取 SQL Server 資料庫專用的物件,在存取 SQL Server 資料庫方面有做過效能上的調整。 • 一般來說,若要存取 SQL Server 資料庫,應選用 System.Data.SqlClient 命名空間的物件,若要存取 SQL Server 以外的資料庫,則應選用 System.Data.OleDb 命名空間的物件。

  10. 類別名稱 (1) • 實際的類別名稱如下:

  11. 類別名稱 (2) • 宣告的敘述是有區別的 ' 使用System.Data.OleDb的物件Dim conn As OleDbConnectionDim cmd As OleDbCommandDim dr As OleDbDataReaderDim adp As OleDbDataAdapter ' 使用System.Data.SqlClient的物件Dim conn As SqlConnectionDim cmd As SqlCommandDim dr As SqlDataReaderDim adp As SqlDataAdapter

  12. 9-2 資料庫存取之路

  13. 路徑一: Connection - DataAdapter - DataSet - DataGrid 1.Connection 物件開啟 Sample.mdb 資料庫。 2. DataAdapter 物件開啟「成績單」資料表。 3.DataSet 物件存放「成績單」資料表。 4.DataGrid 控制元件顯示「成績單」資料表。 5.關閉 Sample.mdb 資料庫。

  14. Route01.aspx 節錄Part I <%@ Import Namespace="System.Data" %> <%@ Import Namespace="System.Data.OleDb" %> <script Language="VB" runat="server"> Sub Page_Load(sender As Object, e As EventArgs) ' 相關物件的宣告Dim Conn As OleDbConnection ' 宣告一個 Connection 物件 Dim Adpt As OleDbDataAdapter ' 宣告一個 DataAdapter 物件 Dim Ds As DataSet ' 宣告一個 DataSet 物件

  15. Route01.aspx 節錄Part II ' Connection 物件開啟 Sample.mdb 資料庫 Dim Provider = "Provider=Microsoft.Jet.OLEDB.4.0" Dim Database = "Data Source=" & Server.MapPath( "Sample.mdb" ) Conn = New OleDbConnection( Provider & ";" & DataBase ) Conn.Open() ' DataAdapter 物件開啟「成績單」資料表 Dim SQL = "Select * From 成績單" Adpt = New OleDbDataAdapter( SQL, Conn )

  16. Route01.aspx 節錄Part III ' DataSet物件存放「成績單」資料表 Ds = New Dataset() Adpt.Fill(Ds, "成績單") ' DataGrid控制元件顯示「成績單」資料表 MyGrid.DataSource = Ds.Tables( "成績單" ).DefaultView MyGrid.DataBind() ' 關閉Sample.mdb資料庫 Conn.Close() End Sub </script>

  17. Route01.aspx

  18. 相關物件的宣告 (1) • 以上程式共使用了四種物件 -- Connection、DataAdapter、DataSet及DataGrid,除了 DataGrid 會安插於 HTML 網頁之外,其他三個物件必須由程式來宣告,其敘述如下: Dim Conn As OleDbConnection ' 宣告一個 Connection 物件 Dim Adpt As OleDbDataAdapter ' 宣告一個 DataAdapter 物件 Dim Ds As DataSet ' 宣告一個 DataSet 物件

  19. 相關物件的宣告 (2) • 由於以上的OleDbConnection、OleDbDataAdapter 及 DataSet 物件分別附屬於 System.Data.OldDb 及 System.Data,所以網頁的最前面還要增加「匯入命名空間」的標示,如下: <%@ Import Namespace="System.Data" %><%@ Import Namespace="System.Data.OleDb" %>

  20. Connection 物件開啟Sample.mdb資料庫 • 此一階段所撰寫的程式如下: Dim Provider = "Provider=Microsoft.Jet.OLEDB.4.0" Dim Database = "Data Source=" & Server.MapPath( "Sample.mdb" ) Conn = New OleDbConnection( Provider & ";" & DataBase ) Conn.Open()

  21. DataAdapter 物件開啟「成績單」資料表 • 此一階段所撰寫的程式如下:

  22. DataSet 物件存放「成績單」資料表 • 此一階段所撰寫的程式如下: Ds = New Dataset()Adpt.Fill(Ds, "成績單")

  23. DataGrid 控制元件顯示「成績單」資料表 • 此一階段所撰寫的程式如下 MyGrid.DataSource = Ds.Tables( "成績單" ).DefaultViewMyGrid.DataBind()

  24. 關閉Sample.mdb 資料庫 • 此一階段所撰寫的程式只有一行: Conn.Close()

  25. 路徑二: Connection - Command - DataReade 1. Connection 物件開啟 Sample.mdb 資料庫。 2. Command 物件開啟「成績單」資料表。 3. DataReader 物件連結「成績單」資料表。 4. 利用 DataReader 物件逐欄逐列讀取資料表,然後填入輸出用的表格。 5. 關閉 Sample.mdb 資料庫。

  26. Route02.aspx 節錄Part I Sub Page_Load(sender As Object, e As EventArgs) ' 相關物件的宣告 Dim Conn As OleDbConnection ' 宣告一個 Connection 物件 Dim Cmd As OleDbCommand ' 宣告一個 Command 物件 Dim Rd As OleDbDataReader ' 宣告一個 DataReader 物件 ' Connection 物件開啟 Sample.mdb 資料庫 Dim Provider = "Provider=Microsoft.Jet.OLEDB.4.0" Dim Database = "Data Source=" & Server.MapPath( "Sample.mdb" ) Conn = New OleDbConnection( Provider & ";" & DataBase ) Conn.Open()

  27. Route02.aspx 節錄Part II ' Command 物件開啟「成績單」資料表 Dim SQL = "Select * From 成績單" Cmd = New OleDbCommand( SQL, Conn ) ' DataReader 物件連結「成績單」資料表 Rd = Cmd.ExecuteReader() ' 利用DataReader物件逐欄逐列讀取資料表,然後填入輸出用的表格 OutputToTable( Rd ) ' 關閉 Sample.mdb 資料庫 Conn.Close() End Sub

  28. Route02.aspx 節錄Part III Sub OutputToTable( Rd As OleDbDataReader ) Dim I As Integer Dim row As TableRow Dim cell As TableCell ' 將資料表的「抬頭」填入表格中 row = New TableRow() row.BackColor = Drawing.Color.Yellow For I = 0 To Rd.FieldCount - 1 cell = New TableCell() cell.Text = Rd.GetName(I) row.Cells.Add( cell ) Next Table1.Rows.Add( row )

  29. Route02.aspx 節錄Part IV ' 逐列讀出資料表,然後填入表格中 While Rd.Read() row = New TableRow() For I = 0 To Rd.FieldCount - 1 cell = New TableCell() cell.Text = Rd.Item(I) row.Cells.Add( cell ) Next Table1.Rows.Add( row ) End While End Sub

  30. Route02.aspx

  31. Command 物件開啟「成績單」資料表

  32. 9-3 DataReader 物件

  33. 讀取資料表的抬頭

  34. Reader01.aspx Part I <%@ Import Namespace="System.Data" %> <%@ Import Namespace="System.Data.OleDb" %> <script Language="VB" runat="server"> Sub Page_Load(sender As Object, e As EventArgs) Dim Conn As OleDbConnection Dim Cmd As OleDbCommand Dim Rd As OleDbDataReader Dim I As Integer Dim Provider = "Provider=Microsoft.Jet.OLEDB.4.0" Dim Database = "Data Source=" & Server.MapPath( "Sample.mdb" ) Conn = New OleDbConnection( Provider & ";" & DataBase )

  35. Reader01.aspx Part II Conn.Open() Dim SQL = "Select * From 成績單" Cmd = New OleDbCommand( SQL, Conn ) Rd = Cmd.ExecuteReader() Msg.Text = "成績單:<UL>" For I = 0 To Rd.FieldCount - 1 Msg.Text &= "<LI>" & Rd.GetName(I) & "</LI>" Next Msg.Text &= "</UL>" Conn.Close() End Sub </script>

  36. Reader01.aspx Part III <Html> <Body BgColor="White"> <H3>讀取資料表每一個欄位的抬頭,並將其顯示在網頁上 <HR></H3> <Form runat="server"> <asp:Label runat="server" id="Msg" /> </Form> <p> <HR></Body> </Html>

  37. 讀取資料表的所有資料列 • 利用 DataReader 物件依序讀取資料表的所有資料列,其程式架構大致如下: ‘ 每次讀取資料之前都要先呼叫 Read(),若含有資料,則傳回 TrueWhile DataReader.Read() ’利用 DataReader 物件所提供的屬性或方法讀取目前資料列End While

  38. Item 屬性(1) • 在 While 迴圈中,讀取各欄位的資料時,會使用到 Item 屬性,Item 屬性可用來讀取某一欄位的內容,格式有兩種: DataReader.Item(欄位順序) DataReader.Item(欄位名稱)

  39. Item 屬性(2) • 以「成績單」資料表的第一筆資料為例,其內容如下:

  40. Item 屬性(3) • 而以下是讀取 Item 屬性的結果: DataReader.Item(0) 等於 850301DataReader.Item(1) 等於 "陳桶一"DataReader.Item(2) 等於 90DataReader.Item(3) 等於 76DataReader.Item(4) 等於 98DataReader.Item("學號") 等於 850301DataReader.Item("姓名") 等於 "陳桶一"DataReader.Item("國文") 等於 90DataReader.Item("英文") 等於 76DataReader.Item("數學") 等於 98

  41. Reader02.aspx

  42. Reader02.aspx Part I <%@ Import Namespace="System.Data" %> <%@ Import Namespace="System.Data.OleDb" %> <script Language="VB" runat="server"> Sub Page_Load(sender As Object, e As EventArgs) Dim Conn As OleDbConnection Dim Cmd As OleDbCommand Dim Rd As OleDbDataReader Dim I As Integer Dim Provider = "Provider=Microsoft.Jet.OLEDB.4.0" Dim Database = "Data Source=" & Server.MapPath( "Sample.mdb" ) Conn = New OleDbConnection( Provider & ";" & DataBase )

  43. Reader02.aspx Part II Conn.Open() Dim SQL = "Select * From 成績單" Cmd = New OleDbCommand( SQL, Conn ) Rd = Cmd.ExecuteReader() While Rd.Read() Msg.Text &= "<UL>" For I = 0 To Rd.FieldCount - 1 Msg.Text &= "<LI>" & Rd.GetName(I) & " = " & _ Rd.Item(I) & "</LI>" Next Msg.Text &= "</UL>" End While Conn.Close() End Sub </script>

  44. Reader02.aspx Part III <Html> <Body BgColor="White"> <H3>讀取資料表的所有資料列 <HR></H3> <Form runat="server"> <asp:Label runat="server" id="Msg" /> </Form> <p> <HR></Body> </Html>

  45. 讀取資料列的其他方法 • 除了 Iten 屬性之外,我們可能需要藉助以下屬性來讀取或判斷各欄位資料的內容:

  46. GetDataTypeName 方法 (1) • 讀取某一欄位的資料型別,以「成績單」資料表為例,各欄位的型別如下:

  47. GetDataTypeName 方法 (2) • 呼叫 GetDataTypeName 方法的傳回值如下:

  48. GetOrdinal 方法 • 讀取某一欄位名稱對應的欄位編號,以「成績單」資料表為例,幾個欄位名稱對應的編號是「學號=0、姓名=1、國文=2、英文=3、數學=4」,所以: DataReader.GetOrdinal("學號") 傳回 0 DataReader.GetOrdinal("姓名") 傳回 1 DataReader.GetOrdinal("國文") 傳回 2 DataReader.GetOrdinal("英文") 傳回 3 DataReader.GetOrdinal("數學") 傳回 4

  49. IsDbNull 方法 • 判斷某一欄位內容是否為 Null(沒有資料即為 Null) ,假設目前所讀取的資料中,每一個欄位都含有資料,則結果如下: DataReader.IsDbNull(0) 等於 FalseDataReader.IsDbNull(1) 等於 FalseDataReader.IsDbNull(2) 等於 False…

  50. GetValues 方法(1) • 讀取所有欄位的內容。Item屬性每次只能讀取一個欄位的資料,而 GetValues 方法則可以一次讀取所有欄位的資料,方法如下: ' 宣告一個陣列,而陣列元素的數目與欄位數相等Dim Fields(DataReader.FieldCount-1) ' 讀取目前資料錄的所有欄位DataReader.GetValues(Fields)

More Related