綜合應用-移動、更新與刪除記錄
期末程式作業
請撰寫下列表單程式,將資料表basic及score相對應,顯示每個 學生之各項成績及平均成績。最後在表單右邊統計全班總成績,
並且依平均成績及格與否繪製成績圖餅圖。
註:此份作業請依公告之作業繳交說明完成。
使用ADO元件
新增控制項
Microsoft ADO Data Control 6.0
Adodc
Microsoft DataGrid Control 6.0
DataGrid
按選單的project ->component
Adodc
按右鍵ÆADOCD屬性
Adoce
工具出現兩個新的控制項
按一下 adoce控制項
Adodc (cont.)
提供者
Microsoft Jet 4.0 OLE DB Provider
連接
選擇資料庫
Adodc (cont.)
查取表格:修改 RecordSource
CommandType=2-adCmdTable
Table=Authors
下指令查詢:修改 RecordSource
CommandType=1.adCmd Text
CommandText:SQL指令
設定資料來源是哪一 個表格
DataGrid
新增資料感知元件 新增data grid 控制項
Adodc的 caption
DataGrid
設定資料感知元件
DataSource=資料控制項
Ex.設定data grid的屬性DataSource=adodc1
Adodc的 caption
DataGrid (cont.)
滑鼠在
dataGrid上按 右鍵
Data Grid進階設定
Delete/Insert:刪除、增加顯示的欄位
切割
DataGrid (cont.)
Data Grid切割
DataGrid (cont.)
Retrieve Fields:依資料庫內容決定欄位
其他可和acodc1配合使用的控制項
使用資料感知元件
Text
Label
DataSource
DataField
DataList
DataCombo
RowSource
ListField
Exercise
讀取學生基本資料表
1. 新增學生基本資料的mdb檔
2. 從mdb檔讀取所有學生資料表,顯示在 form上
3. Go!!
這樣就夠了嗎?
如果我只列出要一部份符
合條件的資料怎麼辦?
Structured Query Language
資料庫存取的標準語言
有些資料庫管理系統使用修改過的SQL Select 欄位名[,欄位名……]
From 表格名 例
Select Author
From Authors Select Author From Authors
SQL (cont.)
在adodc中
RecordSource=1-adCmdText
CommandText=select author from authors
SQL命令
SQL (cont.)
所得的結果存在adodc的recordset中 條件式select: 使用where
Select Au_ID, Author From Authors Where Au_ID>100
可搭配 AND, OR使用
Select Au_ID, Author From Authors Where AuID>100 AND Au_ID<200
SQL (cont.)
Like: 相似字串
Select Au_ID, Author From Authors Where Author Like ‘A%’
Like後面的字串樣板用單引號括起來
%代表任意字元
動態產生、執行SQL
Adodc.recordsource=“SQL語法”
Adodc.refresh
為了美觀,
adodc1的visible 可設為false
Private Sub Command1_Click() Dim min, max As Integer
Dim name As String Dim sqlcmd As String min = Int(Text1.Text) max = Int(Text2.Text) name = Text3.Text
sqlcmd = "select au_id, author ” + _
“from authors " + _
"where (au_id>=" + Str(min) + _
" and au_id<=" + Str(max) + ") + _ and author like ('" + name + "')"
Adodc1.RecordSource = sqlcmd Adodc1.Refresh
End Sub
動態產生、執行SQL (cont.)
Select au_id, author From authors
Where (au_id>=1
and au_id<=100) and author Like ‘b%’
Î
“Select au_id, author ” + _
“From authors ” + _
“Where (au_id>=1 and ”+
“au_id<=100) and ” + _
“author Like ‘b%’”
動態產生、執行SQL (cont.)
Î
“Select au_id, author ” + _
“From authors ” + _
“Where (au_id>=” +str(min)+ “ and ” + _
“au_id<=” +str(max)+ “) and ” + _
“author Like ‘” +name+ “’”
ODBC與專業資料庫
使用ODBC可以讓 程式設計師不去理 會資料庫的位置、
形態
在那個server……
是Oracle, SQL Server 或是
Access……
資料庫控制項
資料庫 ODBC
ODBC與資料庫 (cont.)
專業資料庫 (Lock, Cache, Backup)
SQL Server, Oracle……
常有獨立的伺服器
效能較好,功能較強
一般資料庫
Access, FoxPro……
只定義檔格式
執行SQL的 方式不同
使用專業資料庫
使用專業資料庫 (cont.)
使用ODBC (cont.)
控制台ÆODBC資料來源