Title: ?13? VB?????
1?13? VB?????
????!!!
2????VB??????????
- VB???????????
- VB????????????
- ??????????????
3VB????????
- Access?????(Windows Office??,?.mdb??????)?
- ??????dBASE, Foxpro???????????,VB?????Excel??????
????? - ODBC?????????????????(ODBC)????, ? SQL
Server, Oracle, Sybase???????
4??????
- VB????????????????
- DataGrid?????(???)
- ??ADO???????
5????????????
VB ?? ??
???
????
????????????
6VB????????
- ???????(VB?????????,?????,?????????)
- ???????????(????)
- ??????,DAO??(??)
- ActiveX??????,ADO??(???)
7VB????????
- ????????,???????????????????????
8??????????
- ????????????,????????????????
- ?????????????????????????
- ????????VB??????
913.1 ???????(VB??)
- ?????Access??????,??????,??VB???????,?????????
- ??????
- ?????
- ????????
- ??????????
- ????????
- ?????????????????????????????
1013.1.1 ??????? ?????
- ??????????????????
- ??
- ???????,??????????
- ?????????
- ?????????,????????????,????????,?????????
- ??????????,???????
- ????,????????,??????
1113.1.2 ??????? ?????
- ??????????,?????????,
- ???????????????????
- ?????????
- ?????????????
- ?????????????,????????????????
- ?????????,?????,??????,?????????,?????????????
1213.1.3 ??????? ?????
- ??????,??????????????????????,????????????,???????
??????,??????????????????? - ???????(??????)
- ??Data????
- ???Data????
- ??DBGrid????(??)
1313.1.4 ??????? ?????
- ???????,?????????????????
- ?????????
- ?????????(?????)
- ??Data????
- ???Data????
- ??DBGrid????(??)
1413.1.5 ??????? ?????
- ??????,??????????
- ?SQL?????????Select??
- ?????????????
1513.1.6 ??????? ????
- ????????????/??
- ?????(???????????????????,??????,????)
- ?????(?????????)
16?????????
??
??????
??ADO??
??Recordset??
????VB????
1713.2 ???????????
- ???????
- Data??(?)
- ADO????(?) ?
- ?????? ?
1813.2.1 ADO????
- ADO????????????????? ??,??????????????,??????
??,?????? - ???????? ?????Microsoft ADO Data Control
13.0
19ADO??????????
- ??????????????
- ?????????(????????????????????,??????),?????SQL???
? - ??????????????, ??????????????,?????(????????)
- ?????
20ADO?????????(??)
- ConnectionString??
- ??????,????????
- ??????????????(?????????),????????,?????????
- ?????
- OLE DB??(.UDL)
- ODBC???(DSN)
- ??????????????
- ????????ADO???????????
21ADO?????????(??)
- RecordSource??
- ???????,??????????????????
- ?????
- ?????????(CommandType??adCmdTable)
- ????Select??(CommandType??adCmdText)
- ????????ADO???????????
22ADO?????????(??)
- CommandType??
- ???????
- ??
- adCmdTable
- adCmdText
- adCmdStoredProc
- adCmdUnknown
- ????????ADO???????????
23ADO????????(??)
- Recordset??
- ?ADO??????????????????,??Recordset????????????????
????????????? - Recordset??,???????,????????????,?????????,??????,
????,?????????? - ????????????(???,recordsource)??????
- Recordset?????????????,????????
24?????
????? (???)
???? (????)
25??
- ADO???????????????,???????,?????????
- ADO??????
2613.2.2 ?????????
- ADO???????????????,???????,??????????????????????
????? - ?????????????,??????????????????????
- ??????
- ??????????????
- ???????
- ?????????????
27???????????
- ??????
- ?CheckBox, ListBox , TextBox,Label,PictureBox,Imag
e,ComboBox?,????DataSource?DataField????????????(?
??) - ??????
- ?DBCombo, DBGrid, DBList, RichText, FlexGrid?
- ???OLEDB???????(?)
- ?DataCombo, DataList, DataGrid?
28?????????????
- ???????
- DataSource??, ????????????,?ADO???????
- ??????????????????
- ??????????ADO????,?????????
- DataField??, ???????????(???????)
- ??????????????????
2913.2.3 ??ADO???????????
- ?????ADO????
- ???????ADO?????????,???????????????(??????)
- ????????, ??????????????, ?????????????ADO????????
- ?????ADO????????????????????(?????recordset??????)
- ??????????????????
30ADO????????
- ??????????,??????
- ?????Stu.mdb?????Student??
- (???Student??Course??SC???)
- ?????Examp1?
31?
- ?????????????????,??????????????
- ??????ADO?????RecordSource????Select???
32ADO????????
- ?????????????????
- ??ADO?????RecordSource?
- Select
- Student.stuID,StuName,stuSex,stuDept,couName,st
uScore - from Student,Course, SC
- where Student.stuIDSC.stuID And
- SC.couIDCourse.couID
- ??CommandType???adCmdText
- ?????????DataSource?DataField??
- ???Examp2?
33??,????ADO?????????,?????????????,????????????????
?????,????
?
- ???????ADO????????????,???????????????
- ???????ADO???????Recordset?????????
3413.2.4 Recordset??(???)???
- Recordset ?????
- EOF?BOF??
- ???Fliter??
- RecordCount??
- AbsolutePosition??
- Bookmark??
35 ??????(??)
- RecordCount????????????
- AbsolutePosition????????????????(????????),?1????
36??????(??)
- BOF???????????????
- EOF???????????????
- ???????
- SumAge 0
- adodc1.Recordset.MoveFirst
- Do While not adoc1.Recordset.EOF
- SumAge SumAge adodc1.Recordset(age)
- adodc1.Recordset.MoveNext
- Loop
- AverageAge SumAge/adodc1.Recordset.RecordCount
37??????(??)
- Bookmark???????????,????,????????????????,???????
???? - Dim myBookMark as Variant
- myBookMark adodc1.Recordset.BookMark
????????????????????, ???????????????
38??????(??)
- ??Recordset??????
- ??Recordset???Filter??,???Recordset???????????????
?,???????Recordset?????,??????????????????????????
,????????Recordset???????????? - ??
- ??????,??????
- ?????????
39?????
- ??
- adodc1.Recordset.Filter ?????
- ????????
- ??? ????? ??(?????)
40Filter?????
- ?????????,??????????
- ?????
- ?????(gt, lt, lt, gt, ltgt, , LIKE)
- ???,???(), ??(),LIKE(,)
- ??AND,OR????(??)
- ?
- adodc1.Recordset.Filter ProductID gt 30 and
ProductID lt50 - adodc1. Recordset .Filter ProductName LIKE
computer
41?????
- ??
- Adodc1.Recordset.Filter adFilterNone(??????)
42??????(??)
- Fields??????????
- ??????????????,??????
- ????,????????????Field??
- Text1.text adodc1.Recordset.Fields(0).Value
- Text2.text adodc1.Recordset.Fields(Orderid).V
alue - Text1.text adodc1.Recordset(Orderid)
- ?????????????,???????????
- ?????????????
- (0)??????????
- ()??????????
43Field??
- ???????,Recordset????????
- ????
- Name?????
- Value?????????
- OrdinalPosition???Field??????
- Type?Size???????????
- SourceField, SourceTable??????????????????,??????
???????????????
4413.3.2 Recordset??(???)???
- Recordset ?????
- Move??
- MoveFirst?MoveLast?MoveNext?MovePrevious
- ??????
- AddNew?Update?Delete?CancelUpdate?
- Find??
45??Move???
- ???ADO?????????,???????
- MoveFirst?????????
- MoveLast?????????
- MoveNext??????????????
- MovePrevious??????????????
- ??
- MoveNext?MovePrevious??????????????
- ?????(BOF, EOF),?????????,????
- ?????????
46???
- ??????,????????
- ??????????
- ????????????????
???????Examp3?
47??????(??)
??Find?? ?????????????????? ?adodc1.Recordset.F
ind(Orderid1101) adodc1.Recordset.Find(
Name Like ? )
48??????(??)
??AddNew??(?????????) ???????,?????,????,?? ?????
??????,?????????? adodc1.Recordset.AddNew adodc
1.Recordset(Orderid) 100 adodc1.Recordset(Or
derdate) 2001/06/30 adodc1.Recordset(ShipAd
dress) Shanghai
?????????????????????
49??????(??)
??Update?? ??????????????????????????????????????
?????,??????????? adodc1.Recordset.AddNew adodc1
.Recordset(Orderid) 100 adodc1.Recordset(Ord
erdate) 2001/06/30 adodc1.Recordset(ShipAdd
ress) Shanghai adoc1.Recordset.Update
??????ADO??????????????,??????????
50??????(??)
??Delete?? ??????,????
adodc1.Recordset.Delete
51??????(??)
- ??CancelUpdate??
- ????????Update????????????????????
- adodc1.Recordset.CancelUpdate
52???????
- ??????????
- ??????????
- ????????
- adodc1.Recordset.MoveNext
- adodc1.Recordset(Orderid) 101
- adodc1.Recordset(Orderdate) 2001/06/30
- adodc1.Recordset(ShipAddress) Chongqing
- adodc1.Recordset.Update
- ??????????????????
53???
- ??????
- ???AddNew????
- ???Delete????
- ???Update????
- ???CancelUpdate????
- ???Find????,???????????????????,????????????????
?? - ??Filter???????????????
5413.4 DataGrid?????
????
- ???????????????????????,??????????????????,???????
???,??????
55??
- ??DataGrid?????????????
- ??DataGrid???????????
- ???????????,????? ?????Microsoft DataGrid
Control 13.0(OLEDB)? -
56DataGrid?????
- ??????????,???????? ?,??????????
- ????
- DataSource?????,??????? (??????)
57DataGrid?????
- ????????,?????????????
- ?????????????Properties?Columns????
- ??????????????????????Edit,??????????,????
- ???????????? ?Splits????
- ????????????????????????
58DataGrid???????
- ??DataGrid????????????,??????DataGrid???????
- ??DataGrid??????,???????
- AllowAddNew??
- AllowDelete??
- AllowUpdate??
- ColumnHeaders????????????
- Enabled??????DataGrid??,??????????
59ADO????????
- ????????????
- ????
- ???????AdoDC1??
- ??DataGrid??????,??????
- ??DataGrid???????
- ?????
6013.5 Data????
- Data??
- ??????????????
- ?????????,?????SQL????
- ??????????????, ??????????????,?????
- ?????
61??Data?????
- Connect???????
- DatabaseName??????????
- RecordSource???(???????)
- Options?????????????
- Exclusive????????????
- ReadOnly?????????
- EditMode?????????
621?Data?? DatabaseName??
- ?????,????????????
- ?????
- d\student\score.mdb
- ??????????
- \\server\work\score.mdb , ?????
- g\work\score.mdb, ?????????
- ?????????????,????????
632?Data?? Connect??
- ?????,??????????, ?????ODBC???????????
- Access Access(????)
- dBASE III dBASE III
- dBASE 5 dBASE 5.0
- Paradox 5.x Paradox 5.x
- FoxPro 2.6 FoxPro 2.6
- Excel 3.0 Excel 3.0
643?Data?? RecordSource??
- ???????????
- ???
- SQL???????(???????)
- ??
- ????
- SQL????
65??Data?????
- Move??
- MoveFirst?MoveLast?MoveNext?MovePrevious
- ??????
- AddNew?Update?Delete?CancelUpdate?
- Find??
- FindFirst?FindLast?FindNext?FindPrevious
66??Data?????
- Reposition
- ???????????
- Validate
- ?????????????????????, ??, ??, ??, ??
- Error
- Data????????????????????, ??????????????,
?????????????
6713.6 ADO??????
ADO??
??
68ADO???
?
- ADO???????,?????????????????????????,????????????,
???????,??????????????????????
69????????????
????
VB ?? ??
???
????
????????? (ADO)????
70ADO???
?
- ADO???????,?????????????????ADO???????????????????
?? - ??ADO??????????,????????(?????)?
- ???????
- ?????????,?????
- ???????????
71ADO???
?
- ADO?????????????????????????????????????
- ADO??????????????????
- ADO??????????????????????Visual C?Visual
J?Visual InterDev????ADO???
72????ADO????
Connection
Errors
Error
Command
Parameters
Parameter
Recordset
Fields
Field
73ADO????
- Connection ??????????
- Command ??????????????
- Recordset ???????????
- Field ???????????
- Error ????????????????
- Parameter ?????????
- ??????????
?? ????Microsoft ActiveX Data Object 2.0
Library
74?????????
- 1??????Stu.mdb
- Student,Course,SC?
- 2???????????(DSNvbtest)
- ????ODBC?????
- ???????????,?????? ?????????
75????????
- ???????????????,???????
- ???????????????????????
- ??????,????????????????
- ??,??????(????????)?
- ??
- Dim adoRecordset As ADODB.Recordset
- ??
- ???????????????????
- Dim adoRecordset As New ADODB.Recordset
- ?????New???
- Dim adoRecordset As ADODB.Recordset
- Set adoRecordset New ADODB.Recordset
76??ADO???????????
- ??Connection??????????
- ??Command??,???????????????Connection??,??????????
?????????(?Select, Insert, Update?) - ??Command???Execute??????,???????,????????Recordse
t?? - ??Recordset??????
77??ADO???????
- Dim adoConnection As New ADODB.Connection
- Dim adoCommand As New ADODB.Command
- Dim adoRecordset As New ADODB.Recordset
- adoConnection.ConnectionString
"dsnvbtestuidsapwd" - adoConnection.Open
- adoCommand.ActiveConnection adoConnection
- adoCommand.CommandType adCmdTable
- adoCommand.CommandText Student
- adoRecordset adoCommand.Execute
78??ADO???????
- ??Connection??????????
- ??Recordset??,???????????????
- ??Recordset???Open??,?????????
????????????,???????SQL??,????????????????????????
,???????
79??ADO???????
- Dim adoConnection As New ADODB.Connection
- Dim adoRecordset As New ADODB.Recordset
- ????
- adoConnection.ConnectionString
"dsnvbtestuidsapwd" - adoConnection.Open
- ?????
- adoRecordset.ActiveConnection adoConnection
- adoRecordset.CursorLocation adUseClient
- adoRecordset.CursorType adOpenDynamic
- adoRecordset.Source "select from Student"
- adoRecordset.Open , , , , adCmdText
8013.13.1 Connection??
- Connection????????????????
- ??Connection????????????
- ?????
- ??????
- ??Error??????????????
81 ??Connection?????
- ???????Connection????,??????
- ConnectionString ?????,???????????????
- ????????????????,????????,?????
- ConnectionTimeOut ?????????,???0,???????????????
- State ??????????????????????????
82??Connection?????
- Open ????,???????????
- ?
- Dim adoConnection As ADODB.Connection
- Set adoConnection New ADODB.Connection
- adoConnection.ConnectionString
"dsnvbtestuidsapwd" - adoConnection.Open
- ??????Connection???????,????????????????
8313.13.2 Recordset??
- ?ADO???,????Recordset??????????,?????????SQL??????
? - ???Recordset??(???)?????(?)???(?)???
84????? (Recordset??)
????? (???)
???? (????)
85ADO???Recordset?????
- ?????????????,???????/??????????,???????????????
- ???????,???????????????,??????????
- ?????????
86??ADO?Recordset?????
- ?????Command???Execute??????Recordset(???????????
) - Set adoRst adoCom.Execute
- ???????Recordset?????Open??(???????????)
- adoRst.Open
87??Recordset?????
881?CursorType??
- ???????? (???????????)
- Static(adOpenStatic)
- ????????????,??????????
- ????????????????
- Dynamic(adOpenDynamic)
- ?????????????????,?????
- ???????
- ForwardOnly(adOpenForwardOnly)(???)
- ??????????,???????????
- Keyset(adOpenKeyset)
- ???????,???????,??Static?Dynamic??
892?CursorLocation??
- ?????????????
- AdUseClient
- ???????????????
- ???????????????,??????,
- ????????????
- AdUseServer
- ????????????????,??????
- ?????,?????,?????????,?
- ????Bookmark?AbsolutePosition???(???)
903?LockType??
- ?????????????????,?????????????
- AdLockReadOnly(???)
- ??????
- AdLockPessimistic
- ????????????,????????
- AdLockOptimistic
- ?????Update???,?????
- AdLockBatchOptimistic
- ?????????,???????????
914?ActiveConnection??
- ?????????Recordset?????Connection??
925?Source??
- ??????Recordset???????
- 6?MaxRecord??
- ????????Recordset?????????????,???0,??????????
93??Recordset?????
94??Recordset????
- Dim adoRst3 as ADODB.Recordset
- Set adoRst3 New ADODB.Recordset
- adoRst3.ActiveConnection adoConnection
- adoRst3.CursorType adOpenForwardOnly
- adoRst3.CursorLocation adUseClient
- adoRst3.Source Student
- (? adoRst3.SourceSelect From Student)
- adoRst3.Open
95??????
961?????
- ????
- MoveFirst ???Recordset??????
- MoveLast???Recordset???????
- MoveNext?????????????
- MovePrevious?????????????
- BOF???????
- EOF???????
?????????Forward-Only,?????MovePrevious?MoveFirst?
??
972???????
- Recordset???Move????????
- ??Recordset.Move NumRecords, Start
- Numrecords ????????,????,????
- Start ?????????,??????????
- AdBookmarkCurrent????????
- AdBookmarkFirst?????????
- AdBookmarkLast??????????
- ?????BOF?EOF??
98??????
- adoRst.MoveFirst
- Do While not adoRst.EOF
- debug.print adoRst(stuName)
- adoRst.MoveNext
- Loop
99?????????
- ?????,????Recordset???????
- ???????????(??????????????????)
- Textbox????????,?????????,????????
- ??Examp6?
1003?????
- Recordset.AddNew Fields, Values(????)
- Fields Recordset????,?????????????????
- Values Recordset???,???Fields?????
- ????????,??
- adoRst.addNew
- adoRst(couID)c0020
- adoRst(couName)???
- adoRst(couTeacher)Wang
- adoRst(couCredit) 4
1014?????
- Recordset.Delete Affectrecords
- Affectrecords?????????????,??,???????
- AdAffectCurrent ??????(???)
- AdAffectGroup ??????Filter?????????
1025?????
- ???????,?????????????
- ??(??????)
- Recordset.Update Fields, Values(????)
- ???????,??
- ??AddNew???
- ????????
- ???????,?????????,???
- ???????,??????CancelUpdate??
103?????????
- ?????,????Recordset???????
- ???????????(??????????????????)
- Textbox????????,?????????,????????
- ????????????,?????????
- ???????????,????????
- ?????Examp7?
1046?Recordset???????
- ????
- ?????????????,??Update????
- ??????????,??CancelUpdate????
- ???
- ???????????????????,???
- ???????UpdateBatch??,??CancelBatch
- ?????
- ??LockType?????AdLockBatchOptimistic
1057??Recordset???????
- ????????????????,?????Recordset??????????????????
?????????Variant??? - BookMark?????????????
- ??????????????adUseClient(CursorLocation??)
1068???Recordset??????
- ??Recordset???Filter??,???Recordset???????????????
?,???????Recordset??????????,????????Recordset??? - ??recordset.Filter?????
- ??????????? ????? ??
- ??
- ??????,??????
- ?????????
107Filter?????
- ?????????,??????????
- ?????
- ?????(gt, lt, lt, gt, ltgt, , LIKE)
- ???,???(), ??(),LIKE(,)
- ??AND,OR????(??)
- ?
- adoRst.Filter ProductID gt 30 and ProductID
lt50 - adoRst.Filter ProductName LIKE computer
108?????????
- ?Stu?Student????
- ??DataGrid??????
- ????????,??????
- Textbox????????
1099???Recordset????
- Find Criteria, SkipRows,SearchDirection,Start
- Criteria ??????(?Filter??)
- SkipRows ?????????????????
- SearchDirection ??????(??/??)
- Start ?????????
- ??BOF, EOF???True
- ??????????????,?????BOF?
- EOF?True
110ADO???????????
11113.7 ?????????
- ??????SQL Server??????
- ??Excel??
- ??Access???