460 likes | 595 Views
Giới thiệu về Visual Basic và ADO. Nguyễn Văn Khiết. Noäi dung. Giôùi thieäu veà Visual Basic Moâi tröôøng laøm vieäc Caùc kieåu döõ lieäu cô sôû Cuù phaùp caùc leänh Giôùi thieäu veà ADO Moâ hình ñoái töôïng Caùc ví duï söû duïng. Giôùi thieäu veà Visual Basic.
E N D
Giới thiệu về Visual Basic và ADO Nguyễn Văn Khiết
Noäi dung • Giôùi thieäu veà Visual Basic • Moâi tröôøng laøm vieäc • Caùc kieåu döõ lieäu cô sôû • Cuù phaùp caùc leänh • Giôùi thieäu veà ADO • Moâ hình ñoái töôïng • Caùc ví duï söû duïng.
Giôùi thieäu veà Visual Basic • Maøn hình thieát keá form
Giôùi thieäu veà Visual Basic • Xöû lyù caùc söï kieän treân form
VB: Caùc kieåu döõ lieäu cô baûn • Byte • Integer • Long • Currency • Date • String • Boolean • Single • Double
Cuù phaùp caùc leänh • Khai báo biến • Dim TênBiến As KiểuDữLiệu • VD : Dim i as Integer Dim str as String Dim b as Boolean Dim cn as ADODB.Connection Dim lb as ListBox
Cuù phaùp caùc leänh • Leänh gaùn • Cuù phaùp : • Gaùn caùc kieåu döõ lieäu cô sôû Bieán = giaùtrò • Gaùn caùc ñoái töôïng Set bieán = ñoái töôïng • Ví duï : a = 100 Set cn = new ADODB.Recordset
Cuù phaùp caùc leänh • Leänh if • Cuù phaùp : • If điều_kiện Then Lệnh • If điều_kiện Then … End if • If điều_kiện Then … Else … End if
Cuù phaùp caùc leänh • Leänh Select Case • Cuù phaùp : Select Case Biến Case giá_trị_1 (Các) lệnh Case giá_trị_2 (Các) lệnh Case Else (Các) lệnh End Select
Cuù phaùp caùc leänh • Leänh For • Cuù phaùp : For bien=Biên_dưới To Biên_trên [step k] (Các) lệnh Next
Cuù phaùp caùc leänh • Leänh While • Cuù phaùp : While điều_kiện_lặp (Các) lệnh Wend Do While điều_kiện_lặp (Các) lệnh Loop
ADO Browser / Application ADO OLE DB ODBC Relational Data Non-Relational Data
ADO (ActiveX Data Object) • Ñeå söû duïng ADO trong Visual Basic, tham chieáu ñeán thö vieän: • Trong Visual Basic, choïn menu Project,References • ChoïnMicrosoft ActiveX Data Objects 2.6 Library .
ADO Object Model • Connection • Recordset • Command • Parameter • Field • …
ADO Object Model • Ñoái töôïng Connection • Thieát laäp keát noái ñeán CSDL. • Xaùc ñònh OLE DB provider • Thöïc hieän 1 caâu query • Quaûn lyù transactions treân keát noái ñang môû.
Connection Object • Ví duï : Môû moät connection (kết nối tới MS Access : hs.mdb) Dim cn As New ADODB.Connection cn.ConnectionString = "Provider = Microsoft.Jet.OLEDB.4.0; Data Source = hs.mdb" cn.Open
Connection Object • Ví duï : Môû moät connection (kết nối tới MS SQLServer : pubs) Dim cn As New ADODB.Connection cn.ConnectionString = “Provider = SQLOLEDB.1;Data Source=SELAB106;User ID=sa;Initial Catalog=pubs" cn.Open
Connection Object • Ví duï : Thöïc hieän caâu leänh SQL ‘ insert statement Dim strSQL as String strSQL = "insert into employee values ('ARD25437A','John','P','Baxter',7, 100, 0877, '1989-11-11T00:00:00 ')" cn. Execute strSQL
Connection Object • Ví duï : Thöïc hieän caâu leänh SQL ‘ update statement Dim strSQL as String strSQL = “UPDATE employee SET job_id=11 WHERE fname='Pedro’ " cn. Execute strSQL
Connection Object • Ví duï : Thöïc hieän caâu leänh SQL ‘ delete statement Dim strSQL as String strSQL = “DELETE from employee WHERE fname='Pedro’ " cn. Execute strSQL
Recordset Object • Recordset object provides methods for manipulating result sets . • Recordset object allows you to add, update, delete, and scroll through rows in the recordset.
Recordset Object • Caùc caùch ñeå laáy ñöôïc moät Recordset • Execute connection • Execute command • Open Recordset.
Recordset Object • Open recordset Open strSQL, connection, opentype, locktype
Recordset Object • Open Type • adOpenForwardOnly • adOpenStatic • adOpenKeyset • adOpenDynamic • Lock type • adLockReadOnly • adLockOptimistic • adLockPessimistic • adLockBatchOptimistic
Recordset Object • VD Dim rs as ADODB.Recordset Set rs = New ADODB.Recordset rs.Open “Select * from employee”, cn, adOpenStatic, adLockOptimistic … Set rs = Nothing
Recordset Object • Di chuyeån treân Recordset • MoveFirst • MoveNext • MovePrevious • MoveLast • Move
Recordset Object • Duyeät qua Recordset Dim rs as ADODB.Recordset Set rs = New ADODB.Recordset rs.Open “Select * from employee”, cn, adOpenStatic, adLockOptimistic If rs.RecordCount>0 then rs.MoveFirst While not rs.EOF … rs.MoveNext WEnd End if Set rs = Nothing
VB & ADO : Code example • Database : Microsoft Access 2000 • Data Source : hs.mdb • Table : HOCSINH
Ví duï 1 • Nhaäp thoâng tin hoïc sinh. • Duøng phöông thöùc AddNew cuûa Recordset
Ví duï 1 • Xöû lyù nuùt Ghi Dim cn As New ADODB.Connection cn.ConnectionString = "Provider = Microsoft.Jet.OLEDB.4.0; Data Source =" & App.Path & "\hs.mdb" cn.Open Dim rs As New ADODB.Recordset rs.Open "Select * from HOCSINH", cn, adOpenStatic, adLockOptimistic rs.AddNew rs!TenHS = txtTenHS.Text rs!NgaySinh = txtNgaySinh.Text rs("Noisinh") = txtNoiSinh.Text rs("DiaChi") = txtDiaChi.Text rs.Fields("GhiChu") = txtGhiChu.Text rs.Update Set rs = Nothing Set cn = Nothing
Ví duï 2 • Nhaäp thoâng tin hoïc sinh. • Duøng phöông thöùc Execute cuûa Connection.
Ví duï 2 • Xöû lyù nuùt Ghi Dim cn As New ADODB.Connection cn.ConnectionString = "Provider = Microsoft.Jet.OLEDB.4.0; Data Source =" & App.Path & "\hs.mdb" cn.Open Dim strSQL As String strSQL = "Insert into HOCSINH(TenHS,NgaySinh,NoiSinh,DiaChi,GhiChu) values ('" & txtTenHS.Text & "','" & txtNgaySinh.Text & "','" & txtNoiSinh.Text & "','" & txtDiaChi.Text & "','" & txtGhiChu.Text & "')" cn.Execute strSQL Set cn = Nothing
Ví duï 3 • Laáy danh saùch hoïc sinh. • Duøng MSHFlexGrid ñeå hieån thò
Ví duï 3 • Choïn söû duïng Microsoft Hierrachical FlexGrid Control. • Choïn Project | Components
Ví duï 3 • Khôûi taïo Connection Public Sub KhoiTaoKetNoi() cn.ConnectionString = "Provider = Microsoft.Jet.OLEDB.4.0; Data Source =" & App.Path & "\hs.mdb" cn.Open End Sub
Ví duï 3 • Hieån thò tieâu ñeà Public Sub HienThiTieuDe() gridHS.Cols = 7 gridHS.Rows = 2 gridHS.TextMatrix(0, 0) = "STT" gridHS.TextMatrix(0, 1) = "MaHS" gridHS.TextMatrix(0, 2) = "Teân HS" gridHS.TextMatrix(0, 3) = "Ngaøy sinh" gridHS.TextMatrix(0, 4) = "Noi sinh" gridHS.TextMatrix(0, 5) = "Ñòa chæ" gridHS.TextMatrix(0, 6) = "Ghi chuù" gridHS.AllowUserResizing = flexResizeBoth End Sub
Ví duï 3 • Laáy döõ lieäu Public Sub LayDuLieu() Dim rs As New ADODB.Recordset rs.Open "Select * from HOCSINH", cn, adOpenStatic, adLockOptimistic If rs.RecordCount > 0 Then gridHS.Rows = rs.RecordCount + 1 rs.MoveFirst Dim i As Integer For i = 1 To rs.RecordCount gridHS.TextMatrix(i, 0) = i gridHS.TextMatrix(i, 1) = rs!MaHS gridHS.TextMatrix(i, 2) = rs!TenHS gridHS.TextMatrix(i, 3) = rs!NgaySinh gridHS.TextMatrix(i, 4) = rs!NoiSinh gridHS.TextMatrix(i, 5) = rs!DiaChi gridHS.TextMatrix(i, 6) = rs!GhiChu rs.MoveNext Next End If Set rs = Nothing End Sub
Ví duï 3 • Goïi 3 haøm treân: Private Sub Form_Load() KhoiTaoKetNoi HienThiTieuDe LayDuLieu End Sub
Ví duï 4 • Laáy danh saùch hoïc sinh. • Duøng ListView ñeå hieån thò
Ví duï 4 • Choïn söû duïng Microsoft Windows Common Control 6.0. • Choïn Project | Components
Ví duï 4 • Khôûi taïo Connection Public Sub KhoiTaoKetNoi() cn.ConnectionString = "Provider = Microsoft.Jet.OLEDB.4.0; Data Source =" & App.Path & "\hs.mdb" cn.Open End Sub
Ví duï 4 • Hieån thò tieâu ñeà Public Sub HienThiTieuDe() lvHS.View = lvwReport lvHS.ColumnHeaders.Add , , "STT" lvHS.ColumnHeaders.Add , , "MaHS" lvHS.ColumnHeaders.Add , , "Teân HS" lvHS.ColumnHeaders.Add , , "Ngaøy sinh" lvHS.ColumnHeaders.Add , , "Nôi sinh" lvHS.ColumnHeaders.Add , , "Ñòa chæ" lvHS.ColumnHeaders.Add , , "Ghi chuù" End Sub
Ví duï 4 • Laáy döõ lieäu Public Sub LayDuLieu() Dim rs As New ADODB.Recordset rs.Open "Select * from HOCSINH", cn, adOpenStatic, adLockOptimistic If rs.RecordCount > 0 Then rs.MoveFirst Dim i As Integer Dim li As ListItem For i = 1 To rs.RecordCount Set li = lvHS.ListItems.Add(, , i) li.SubItems(1) = rs!MaHS li.SubItems(2) = rs!TenHS li.SubItems(3) = rs!NgaySinh li.SubItems(4) = rs("NoiSinh") li.SubItems(5) = rs("DiaChi") li.SubItems(6) = rs.Fields("Ghichu") rs.MoveNext Next End If Set rs = Nothing End Sub
Ví duï 4 • Goïi 3 haøm treân: Private Sub Form_Load() KhoiTaoKetNoi HienThiTieuDe LayDuLieu End Sub
Ví duï 5 • Xoùa moät hoïc sinh töø danh saùch
Ví duï 5 • Xöû lyù nuùt Xoùa Private Sub cmdXoa_Click() Dim li As ListItem Set li = lvHS.SelectedItem If Not IsNull(li) Then Dim sql As String Dim mahs As Integer mahs = li.SubItems(1) sql = "Delete from HOCSINH where MAHS=" & mahs cn.Execute sql lvHS.ListItems.Remove li.index End If End Sub