1 / 31

Visual Basic 程式設計

Visual Basic 程式設計. 講師:戴志華 hana@arbor.ee.ntu.edu.tw 國立台灣大學電機工程研究所. 第十五章 資料庫 III 與軟體開發流程. 使用 ADO 物件. 優點: 使用範圍廣 較有彈性 Project/References/Microsoft ActiveX Data Object 2.5 Library. ADO 物件模型. Connection 物件 Recordset 物件 Command 物件. Connection. 表示一個通往資料來源的開啟連線 資料庫 ODBC 來源

brone
Download Presentation

Visual Basic 程式設計

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. Visual Basic 程式設計 講師:戴志華 hana@arbor.ee.ntu.edu.tw 國立台灣大學電機工程研究所

  2. 第十五章 資料庫III與軟體開發流程

  3. 使用ADO物件 • 優點: • 使用範圍廣 • 較有彈性 • Project/References/Microsoft ActiveX Data Object 2.5 Library

  4. ADO物件模型 • Connection物件 • Recordset物件 • Command物件

  5. Connection • 表示一個通往資料來源的開啟連線 • 資料庫 • ODBC來源 • 其他OLE DB提供者存在的來源

  6. Connection(cont’d) • Dim 變數名As New Connection • 方法: • Open ConnectionString • ConnectionString可參考Adodc • 與資料庫建立連線 • Close • 關閉連線 • Execute • 命令,影響的記錄數量,命令種類

  7. Connection(cont’d) Dim con As New Connection Dim conStr As String Dim numRec As Integer conStr=“Provider=Microsoft.Jet.OLEDB.4.0;DataSource=C:\class13.mdb;Mode=Read| Write;Persist Security Info=False” con.Open conStr con.Execute(delete * from student where name like ‘王%’) con.Close

  8. Connection(cont’d) • Provider : OLE DB Provider名稱 • Data Source:所要連結的SQL Server或MDB資料的名稱 • DSN : 註冊在現行機器上的ODBC來源名稱 • Password : 使用者密碼 • User ID : 使用者名稱 • Persist Security Info :若ADO將使用者ID與密碼存在Data Link中的話,此值為True。

  9. 「全部」頁籤顯示所選取的OLE DB Provider的所有屬性

  10. Connection(cont’d) 若SQL用Select, 會產生RecordSet物件 Dim con As New Connection Dim conStr As String Dim numRec As Integer Dim rs As New RecordSet conStr=“Provider=Microsoft.Jet.OLEDB.4.0;Data source=C:\testdb.mdb;Mode=Read| Write;Persist Security Info=False” con.Open conStr Set rs=con.Execute(“select * from student where name like ‘王%’”, numRec, adCmdText) con.Close 表示被此命令所影響的記錄筆數 Command Type

  11. RecordSet cursor • 資料庫取得的資料放在recordset中

  12. RecordSet(cont’d) 經驗,不一定永遠對 • 使用Connection.Execute產生的RecordSet的Cursor不能向前移

  13. RecordSet(cont’d) Dim con As New Connection Dim conStr As String Dim rs As Recordset conStr = “………………………………………” con.Open conStr Set rs = con.Execute("select * from student” _ , , adCmdText) Print rs.Fields(0) rs.MoveNext Print rs.Fields(”name") rs.MovePrevious Print rs.Fields("id") Error

  14. RecordSet(cont’d) Dim con As New Connection Dim conStr As String Dim rs As New Recordset conStr = “……………………………………” con.Open conStr rs.ActiveConnection = con rs.CursorType = adOpenStatic rs.Open "select * from student" Print rs.Fields(0) rs.MoveNext Print rs.Fields("au_id") rs.MovePrevious Print rs.Fields("au_id") ok

  15. CursorType • 0-adOpenForwardOnly Recordset只能向下移動而已 • 1-adOpenKeyset Recordset的Cursor可以自由的上下移動 • 2-adOpenDynamic • 3-adOpenStatic

  16. Locktype • adLockReadOnly Recordset唯讀,無法接受資料異動 • adLockOptimistic Recordset可接受資料異動

  17. CursorLocation • adUseServer • adUseClient

  18. 軟體開發流程 需求分析 瀑布模型 系統分析 系統設計 系統實作 系統測試 系統維護

  19. 軟體開發流程(cont’d)

  20. 案例探討 • 圖書管理系統 • 使用Biblio.mdb • 功能 • 查詢書籍,作者與出版社 • 新增書籍 • 新增作者 • 刪除書籍

  21. 需求分析 • 目的 • 了使用者真正的需求 • 具體目標 • 取得Use Cases (使用案例) • Use cases • 使用者操作系統完成某一項任務,系統所做的一連串事

  22. 需求分析 • 查詢書籍 • 輸入書籍的ISBN,取得書籍資料 • 輸入書籍的名稱,取得書籍資料 • 查詢作者 • 輸入作者代碼,取得作者資料 • 輸入作者姓名,取得作者資料 • 決定操作介面……

  23. 系統分析 操作 介面 VB Code 資料庫 SQL command

  24. 系統分析(cont’d) Command3_Click Select …… RecordSet 顯示第一筆 Command2_Click 顯示第二筆 Code Database UI

  25. 系統設計 • 四個副函式 • Command1_Click • Command2_Click • Command3_Click • Display顯示目前的記錄

  26. 系統設計(cont’d) • Command3_Click() • 與資料庫連線 • 產生SQL query • 把傳回的RecordSet存到記憶體中 • 中斷與資料庫的連線 • 顯示第一筆資料(呼叫display)

  27. 系統設計(cont’d) • Command1_Click • 顯示前一筆(呼叫display)資料 • Command_Click • 顯示後一筆(呼叫display)資料

  28. 系統實作 Private Sub Command3_Click() Dim con As New Connection Dim conStr As String Dim rs As New Recordset conStr = <<略>> con.Open conStr rs.ActiveConnection = con rs.CursorType = adOpenStatic rs.Open <<略>> records = rs.GetRows() recNo = rs.RecordCount index = 0 display con.Close End Sub

  29. 系統實作(cont’d) Dim records As Variant Dim index As Integer Dim recNo As Integer Private Sub display() Text1.Text = records(0, index) Text2.Text = records(1, index) Text3.Text = records(2, index) End Sub Private Sub Command1_Click() If (index > 0) Then index = index - 1 display End If End Sub

  30. 系統實作(cont’d) Private Sub Command2_click() If (index < recNo - 1) Then index = index + 1 display End If End Sub

  31. 系統測試 • 利用User Case進行測試 • 輸入資料亂數選定 • 合法的資料 • 錯誤的資料 • 如果有錯誤,重回設計或實作階段

More Related