¦¹±Ð¾Ç½Òµ{±Ð±z¨Ï¥Î ADO µ{¦¡³]p¼Ò¦¡¨Ó¬d¸ß»P§ó·s¸ê®Æ¨Ó·½¡Cº¥ý¡A±Ð¾Ç½Òµ{·|»¡©ú§¹¦¨¦¹¤u§@©Ò»Ýªº¨BÆJ¡C±µµÛ¡A±Ð¾Ç½Òµ{¬O¦b
Microsoft Visual Basic ¤¤«½Æ¡F Microsoft Visual C++¡A²£¥Í Visual C++ Extensions; and
Microsoft Visual J++¡A²£¥Í ADO for Windows Foundation Classes (ADO/WFC)¡C
¥Ñ©ó¥H¤U¨â¶µ¦]¯À¡A¦¹±Ð¾Ç½Òµ{¥H¤£¦Pªº»y¨¥¨Ó¼¶¼gµ{¦¡½X¡G
±Ð¾Ç½Òµ{ªº§e²{¤è¦¡
¦¹±Ð¾Ç½Òµ{¬O®Ú¾Ú ADO µ{¦¡³]p¼Ò¦¡¡C¥¦Ó§O°Q½×µ{¦¡³]pªº¨CÓ¨BÆJ¡C°£¦¹¤§¥~¡A¥¦ÁÙ¥H
Visual Basic µ{¦¡½X¤ù¬q¨Ó»¡©ú¨CÓ¨BÆJ¡C³Ì«á¡A«h«·s»¡©ú¨Ã¾ã¦Xµ{¦¡½X¤ù¬q¡A¦¨¬°¤@Ó Visual Basic ½d¨Ò¡C
µ{¦¡½X½d¨Ò·|¥H¨ä¥L»y¨¥¨Ó«ÂЪí¥Ü¡A¦ý¤£·|¦³°Q½×ªº¸ê°T¡C¦bµ{¦¡³]p»y¨¥±Ð¾Ç½Òµ{¤¤ªº¨CÓ¨BÆJ¡A³£¼Ð¥Üµ{¦¡³]p¼Ò¦¡»P»¡©úªº±Ð¾Ç½Òµ{¤¤ªº¬ÛÃö¨BÆJ¡C½Ð¨Ï¥Î¨BÆJªº½s¸¹¡A¨Ó°Ñ·Ó±Ð¾Ç½Òµ{¤¤ªº°Q½×¸ê°T¡C
¥Ñ©ó¦¹±Ð¾Ç½Òµ{¬O¥Ñ¤@¨Ç¤pªºµ{¦¡½X¤ù¬q©Ò²Õ¦¨¡A¦]¦¹±zµLªk°õ¦æ½Òµ{¤¤ªº³o¨Çµ{¦¡½X¡C
ADO µ{¦¡³]p¼Ò¦¡»¡©ú¦p¤U¡C½Ð§Q¥Î¥¦§@¬°±Ð¾Ç½Òµ{ªºÂŹϡC
ADO ª«¥óµ{¦¡³]p¼Ò¦¡
¤U¤@¨B ¨BÆJ
1: ¶}±Ò³s½u
¨BÆJ 1¡G¶}±Ò³s±µ (ADO ±Ð¾Ç½Òµ{)
¥»¨BÆJªº¤u§@
°Q½×
±z¥²¶·n¦³«Ø¥ß¥æ´«¸ê®Æ¥²¶·±ø¥óªº¤èªk¡F¤]´N¬O³s±µ¡C±z³s±µªº¸ê®Æ¨Ó·½«ü©w©ó³s±µ¦r¦ê¡A¦ý¦b³s±µ¦r¦ê¤¤©Ò«ü©wªº°Ñ¼Æ¥i¦]¨CÓ´£¨ÑªÌ
»P¸ê®Æ¨Ó·½¦Ó²§¡C
ADO ¶}±Ò³s±µªº¥Dn¤èªk¬O Connection.Open ¤èªk¡C±z¤]¥i¥H¥Î¤£¦Pªº¤èªk¡A©I¥s
Recordset.Open ±¶®|¤èªk¡A¦b¦P¤@§@·~¤¤¶}±Ò³s±µ¡A¨Ã¸g¥Ñ³s±µµo¥X«ü¥O¡C¥H¤U¬°¨CºØ¤èªk¦b Visual Basic ¤¤ªº»yªk¡G
connection.Open ConnectionString,
UserID, Password, OpenOptions
recordset.Open Source, ActiveConnection,
CursorType, LockType, Options
¤ñ¸û³o¨âºØ¤èªk¥i¤F¸Ñ ADO ¤èªk¹Bºâ¤¸¤@¯ëªº¯S©Ê¡C
¤èªk°Ñ¼Æ¥i¥H¥Î¼ÆºØ¤èªk«ü©w¡C¨Ò¦p¡ARecordset.Open §Q¥Î ActiveConnection °Ñ¼Æ¡A¥¦¥i¥H¬O¹ê»Ú¤Wªº [¦r¦ê]¡B¥Nªí¦r¦êªºÅܼơA©Î¥Nªí¶}±Ò³s±µªº
Connection
ª«¥ó¡C
¦¹±Ð¾Ç½Òµ{¦¡¨Ï¥Î¤å¦r©Ê³s½u¦r¦ê -
"DSN=Pubs;uid=sa;pwd=;". (¸ê®Æ¨Ó·½¬Oª½±µ¥Ñ "DSN=" ÃöÁä¦r«ü©wªº¡C¦p»Ý¸Ô²Ó¸ê°T¡A½Ð°Ñ¾\
Microsoft
OLE DB Provider for ODBC ªº "¦@«¬³s½u¦r¦ê¡¨ ¤@¸`)¡C
«Ü¦hª«¥óªºÄݩʦb¤èªk°Ñ¼Æ³Q¬Ù²¤®É¡A¥i¥H´£¨Ñ¤Þ¼Æ¡C¨Ò¦p¡A±z¥i¥H³]©w Connection ª«¥ó ConnectionString
ÄÝ©Ê¡A´£¨Ñ Connection.Open ªº³s½u¦r¦ê¸ê°T¡AµM«á¬Ù²¤ Open ¤èªkªº ConnectionString
°Ñ¼Æ¡C
¦¹±Ð¾Ç½Òµ{¨Ï¥Î¤U¦C Connection ª«¥ó«Å§i¥H¤Î Open ¤èªk¡G
Dim cnn As New ADODB.Connection
cnn.Open "DSN=Pubs;uid=sa;pwd=;"
¤U¤@¨B ¨BÆJ
2: «Ø¥ß«ü¥O
¨BÆJ 2¡G«Ø¥ß«ü¥O (ADO ±Ð¾Ç½Òµ{)
¥»¨BÆJªº¤u§@
°Q½×
«ü¥O¬O¸ê®Æ´£¨ÑªÌ¥i¥H²z¸Ñªº«ü¥Ü¡A¥¦¥i¥Hקï¡BºÞ²z©Î¾ÞÁa¸ê®Æ¨Ó·½¡C¤£»Ýn¯S®íªº«ü¥O»y¨¥¡A¤£¹L«ü¥O¤@¯ë¬O¥Î
SQL ¼g¦¨ªº¡C¬d¸ß«ü¥O·|n¨D¸ê®Æ´£¨ÑªÌ¶Ç¦^¥]§t¸ê°T¦Cªº Recordset
ª«¥ó¡C
±N«ü¥O«ü©w¬°¡G
½Ð°Ñ¾\¬ÛÃö¥DÃD¡A¤F¸Ñ°Ñ¼Æ¤Æ«ü¥O²²¤ªº°Q½×¡C
¦¹±Ð¾Ç½Òµ{·|¬d¸ß Pubs ¸ê®Æ®w Authors
ªí®æ¤º©Ò¦³ªº¸ê°T¡CCommand ª«¥ó¦b«Å§i®É·|³]©w¤F¶}±Òªº Connection ª«¥ó¥H¤Î«ü¥O¤å¦r¡Cµ{¦¡½X¦p¤U©Ò¥Ü¡G
Dim cmd As New ADODB.Command
Set cmd.ActiveConnection = cnn
cmd.CommandText = "SELECT * from Authors"
¤U¤@¨B ¨BÆJ
3: °õ¦æ«ü¥O
¨BÆJ 3: °õ¦æ«ü¥O (ADO ±Ð¾Ç½Òµ{)
¥Ø«e©Ò¦b¦ì¸m¡K
°Q½×
¤TºØ¥i¶Ç¦^ Recordset
ªº¤èªk¬O Connection.Execute¡BCommand.Execute
¥H¤Î Recordset.Open¡C³o¬O¥¦Ì¦b
Visual Basic ªº»yªk¡G
connection.Execute(CommandText, RecordsAffected,
Options)
command.Execute(RecordsAffected, Parameters,
Options)
recordset.Open Source, ActiveConnection,
CursorType, LockType, Options
³o¨Ç¤èªk·|³Q³Ì¨Î¤Æ¡A¥H«Kµ½¥Î¨ä¯S®íª«¥óªºªø³B¡C
¿é¥X«ü¥O«e¡A¥²¶··t¦a©Î©ú½T¶}±Ò³s½u¡C¨C¤@ºØ¿é¥X«ü¥Oªº¤èªk¥Nªí¤£¦Pªº³s½u¡G
¥t¤@ºØ®t²§´N¬O¤TºØ¤èªk«ü©w«ü¥Oªº¤è¦¡¡G
¨C¤@ºØ¥\¯à©M®Ä¯à¤§¶¡ªº¨ú±Ë¡G
½Ð¬ã¨s³o¨Ç¿ï¶µ¡A¥¦Ì·|±Ä¥Î Recordset «Ü¦hªº¥\¯à¡C¨ä¤¤¯S§O«nªº¦a¤è¬O°Q½×¨Ï¥Î
Microsoft
Cursor Service for OLE DB¡C½Ð°Ñ¾\ Microsoft
Cursor Service for OLE DB¡A¤F¸Ñ¦¹°Q½×¤º²[ªº¸Ô²Ó¸ê®Æ¡C
¦¹±Ð¾Ç½Òµ{·|¦b§å¦¸¼Ò¦¡Åܧó Recordset¡A¦]¦¹·|«ü©w adLockBatchOptimistic ªº LockType¡C§å¦¸³B²z»Ýn
Cursor
Service¡A¦]¦¹ CursorLocation
ÄÝ©Ê·|³]©w¦¨ adUseClient¡C¦]¬° Command ª«¥ó¤w¸g³]©w¦¨¶}±Òªº³s½u¡A©Ò¥H ActiveConnection
°Ñ¼ÆµLªk«ü©w¦b Open ¤èªk¤¤¡C
Recordset ªº«Å§i©M¥Îªk¦p¤U©Ò¥Ü¡G
Dim rs As New ADODB.Recordset
rst.CursorLocation = adUseClient
rst.Open cmd, , adOpenStatic, adLockBatchOptimistic
¤U¤@¨B ¨BÆJ
4: ¾ÞÁa¸ê®Æ
¨BÆJ 4: ¾ÞÁa¸ê®Æ (ADO ±Ð¾Ç½Òµ{)
¥Ø«e©Ò¦bªº¦ì¸m¡K
°Q½×
Recordset
ª«¥ó¤èªk©MÄݩʪº¤j¶q¸ê®Æ¡A¥i¥Î©óÀˬd¡BÂsÄý©M¾ÞÁa Recordset ¸ê®Æ¡C
±z¥i¥H±N Recordset ·Q¹³¬O¸ê®Æ¦C°}¦C¡C¦b«ü©wªº¥ô¦ó®É¶¡¤¤¥i¥HÀˬd©M¾ÞÁaªº¸ê®Æ¦C¬O¥Ø«eªº¸ê®Æ¦C¡A±z¦b
Recordset ªº¦ì¸m¬O¥Ø«eªº¸ê®Æ¦C¦ì¸m¡C¨C¦¸±z²¾¨ì¥t¤@Ó¸ê®Æ¦C¡A¸Ó¸ê®Æ¦C·|Åܦ¨¥Ø«eªº¸ê®Æ¦C¡C
¥Ø«e¦³¼ÆºØ¤èªk·|©ú½T¦a¹C¨«©Î ¡§ÂsÄý¡¨ Recordset (Move
¤èªk)¡C³¡¥÷¤èªk (Find
¤èªk) ¥i¥H³o¼Ë°µ¡A¦ý·|³y¦¨°Æ§@¥Î¡C¦¹¥~¡A³]©w¯S©wªºÄÝ©Ê (Bookmark
ÄÝ©Ê) ¤]¥i¥HÅܧó±zªº¸ê®Æ¦C¦ì¸m¡CRecordset ª«¥ó CursorType
ÄÝ©Ê©Î Open ¤èªk CursorType °Ñ¼Æ¡A·|¨M©w±z¬O§_¥i¥H©¹«e©Î©¹¦^ÂsÄý Recordset¡C
Filter
ÄÝ©Ê¥i¥H±±¨î±z¦s¨úªº¸ê®Æ¦C (¤]´N¬O»¡¡A±z¥i¥H¬Ý¨ìþ¨Ç¸ê®Æ¦C)¡CSort
ÄÝ©Ê¥i¥H±±¦G±zÂsÄý Recordset ¸ê®Æ¦Cªº¶¶§Ç¡C
±z¥i¥H¨Ï¥Î AddNew
¤èªk«Ø¥ß·sªº¸ê®Æ¦C¡A©ÎªÌ¨Ï¥Î Delete
¤èªk§R°£²{¦³ªº¸ê®Æ¦C¡C
Recordset ¦³¤@Ó Fields
¶°¦Xª«¥ó¡A¥¦¬O¥Nªí¦UÓÄæ¦ì©Î¸ê®Æ¦CÄæ¦ì (¸ê®Æ¦æ) ªº Field
ª«¥óªº²Õ¦X¡C½Ð¨Ï¥Î Field ª«¥ó Value
Äݩʨӫü©w©Î¨ú±o¸ê®Æ¡C±z¤]¥i¥H¨Ï¥Î GetRows
¤èªk¦s¨ú¤j¶qªºÄæ¦ì¸ê®Æ¡Cקï Recordset ¤§«á¡A½Ð¨Ï¥Î Update
¤èªk±N©Ò°µªºÅܧó¶Ç¼½¨ì¸ê®Æ¨Ó·½¡C
¦b¦¹±Ð¾Ç½Òµ{¤¤¡A±z·|¡G
½Ð¨Ï¥Î Move ¤èªk±qÀY¨ì§ÀÂsÄý±Æ§Ç¡B¹LÂoªº Recordset¡C·í
Recordset ª«¥ó EOF
ÄÝ©Ê«ü¥Ü±z¤w¸g¨ì¤F³Ì«áªº¸ê®Æ¦C´N°±¤î¡C·í±z½¾\ Recordset ªº®ÉÔ¡A·|Åã¥Ü§@ªÌªº©m¦W©Mì©l¹q¸Ü¸¹½X¡AµM«á±N¹q¸ÜÄæ¦ì¤¤ªº°Ï½X§ï¦¨
¡§777¡¨¡C(¹q¸ÜÄæ¦ì¤¤ªº¹q¸Ü¸¹½X·|®æ¦¡¤Æ¦¨ "aaa xxx-yyyy"¡A¨ä¤¤ aaa ¬O°Ï½X¦Ó xxx ¬O¥æ´«½X)¡C
·í¨C¤@Ó¸ê®Æ¦CÅܧó«á¡A´N¤£¦A²Å¦X¹LÂo¾¹«ü©wªº³W«h¡A©Ò¥H´N¤£·|Åã¥Ü¦b Recordset¡C·í¹LÂo¾¹¸Ñ°£«á¡A©Ò¦³ªº¸ê®Æ¦C·|«·sÅã¥Ü¡C
½Ð°Ñ¾\µ²ºc±Ôz¬ÛÃö¥DÃD¡A¤F¸Ñ
Authors ªí®æªº§G§½¡Cµ{¦¡½X¦p¤U©Ò¥Ü¡G
rst!au_lname.Properties("Optimize") = True
rst.Sort = "au_lname"
rst.Filter = "phone LIKE '415 5*'"
rst.MoveFirst
Do While Not rst.EOF
Debug.Print "Name = "; rst!au_fname; " "; rst!au_lname; _
", Phone = "; rst!phone
rst!phone = "777" & Mid(rst!phone, 4)
rst.MoveNext
rst.Filter = adFilterNone
¤U¤@¨B ¨BÆJ
5: §ó·s¸ê®Æ
¨BÆJ 5: §ó·s¸ê®Æ (ADO ±Ð¾Ç½Òµ{)
¥Ø«e©Ò¦bªº¦ì¸m¡K
°Q½×
±zèèÅܧó¤F Recordset
¼ÆÓ¸ê®Æ¦Cªº¸ê®Æ¡CADO ¤ä´©¨âÓ»P·s¼W¡B§R°£¥H¤Îקï¸ê®Æ¦Cªº¤èªk¡C
²Ä¤@ºØ¤èªk¬O©Ò°µªºÅܧ󤣷|¥ß§Y¤ÏÀ³¨ì Recordset¡A¹ê»Ú¤W·|¤ÏÀ³¨ì¤º³¡ªº³Æ¥÷½w½Ä°Ï¡CY±z¨M©w¤£·QÅܧó¡A«h¦b³Æ¥÷½w½Ä°Ï¤ºªºÅܧó´N·|³Q±Ë±ó¡CY±z¨M©wnÅܧó¡A«h¦b³Æ¥÷½w½Ä°Ï¤ºªºÅܧó·|®M¥Î¨ì
Recordset¡C
²Ä¤GºØ¤èªk¬O©Ò°µªºÅܧó¦b±z«Å§i¸ê®Æ¦Cªº§@·~§¹¦¨®É¡A·|¥ß§Y¶Ç¼½¨ì¸ê®Æ¨Ó·½¡C©ÎªÌ©Ò¦³¹ï¤@²Õ¸ê®Æ¦C©Ò°µªºÅܧó·|¶°¦X°_¨Ó¡A¤@ª½¨ì±z«Å§i§@·~§¹¦¨¬°¤î (¤]´N§å¦¸¼Ò¦¡)¡CLockType
ÄÝ©Ê·|¨M©w¤°»ò®ÉÔ©Ò°µªºÅܧó·|¤ÏÀ³¨ì¤U¼hªº¸ê®Æ¨Ó·½¡CCursorLocation
ÄÝ©Ê¥i¥H¼vÅTþ¨Ç LockType ³]©w¥i¥HÅã¥Ü¥X¨Ó¡C¨Ò¦p¡AY CursorLocation Äݩʳ]©w¦¨ adUseClient¡A´N¤£¤ä´©
adLockPessimistic ³]©w¡C
¦b§Y®É¼Ò¦¡¤U¡A¨C¦¸©I¥s Update
¤èªk´N·|±N©Ò°µªºÅܧó¶Ç¼½¨ì¸ê®Æ¨Ó·½¡C¦b§å¦¸¼Ò¦¡¤U¡A¨C¦¸©I¥s Update ©Î²¾°Ê¥Ø«eªº¸ê®Æ¦C¦ì¸m¡A´N·|±N©Ò°µªºÅܧóÀx¦s¨ì³Æ¥÷½w½Ä°Ï¡A¦ý¥u¦³ UpdateBatch
¤èªk¤~·|±N©Ò°µªºÅܧó¶Ç¼½¨ì¸ê®Æ¨Ó·½¡CRecordset ·|¦b§å¦¸¼Ò¦¡¤U¶}±Ò (adLockBatchOptimistic)¡A¦]¦¹·|¦b§å¦¸¼Ò¦¡¤U°õ¦æ§ó·s§@·~¡C
µø±¡ªp¤£¦P¡A§ó·s§@·~¥i¥H¦b¥æ©ö¤¤°õ¦æ¡C¥æ©ö·|«Ø¥ß¤@ºØÀô¹Ò¡A¥æ©ö¤¤ªº§@·~¥i¥H¥þ³¡¦¨¥\©Î¥þ³¡¨ú®ø¥¦Ìªºµ²ªG¡C
´¶³qªº¥æ©ö½d¨Ò¬O»È¦æÀ³¥Îµ{¦¡¡A·í§@·~±q¬Y¤@Ó±b¤á®¿¥Xª÷ÃB¡A¥t¤@Ó§@·~±N¬Û¦Pªºª÷ÃB¦s¤J¥t¤@Ó±b¤á®É¡AÂù¤è¥²¶·¦P®É¦¨¥\¡CY¨ä¤¤¤@¶µ§@·~¥¢±Ñ¡A«h¥t¤@¶µ§@·~¥²¶·ÁÙì¡A§_«h±b¤á·|¥X²{¦¬¤ä¤£¥¿Å¡C
¥æ©ö¤@¯ë·|°t¸m©M«O¯d¸ê®Æ¨Ó·½¦³ªº¸ê·½¨Ñªø´Á¨Ï¥Î¡C°ò©ó³oÓ²z¥Ñ¡A«Øij±z¥æ©ön¶V²µu¶V¦n¡C(³o¤]¬O¬°¤°»ò¦¹±Ð¾Ç½Òµ{¤£·|¦b³s½u«Ø¥ß«á¥ß§Y¶}©l)¡C
´N¹ê¾Ô¸gÅç¨Ó»¡¡A¦¹±Ð¾Ç½Òµ{¤£»Ýn¥æ©ö¡A¤£¹L·|¥Î¤@Ó¥æ©ö¨Ñ©Ò¦³¥Ü½d¥Î³~°Ñ¦Ò¡C±Ò°Ê¥æ©ö©M°õ¦æ§å¦¸§ó·sªºµ{¦¡½X¦p¤U©Ò¥Ü¡G
cnn.BeginTrans
rst.UpdateBatch
¤U¤@¨B ¨BÆJ
6: µ²§ô§ó·s
¨BÆJ 6: µ²§ô§ó·s (ADO ±Ð¾Ç½Òµ{)
¥Ø«e©Ò¦bªº¦ì¸m¡K
°Q½×
·Q¹³§å¦¸¥æ©öµo¥Í¿ù»~¦Óµ²§ô»~¡C±z¦p¦ó¨Ì¾Ú¿ù»~ªº¥»½è©MÄY«©Ê¥H¤ÎÀ³¥Îµ{¦¡ªºÅÞ¿è¨Ó¸Ñ¨M¿ù»~¡C¤£¹L¡AY¸ê®Æ®w»P¨ä¥L¨Ï¥ÎªÌ¦@¥Î¡A¨å«¬ªº¿ù»~´N¬O¦³¨Ç¤H¦b±zק蠟«e¡A´N¥ýקï¤FÄæ¦ì¡C³oºØ¿ù»~ºÙ¬°½Ä¬ð¡CADO ·|°»´ú¦¹ª¬ªp«á³ø§i¿ù»~¡C
±Ð¾Ç½Òµ{¤¤ªº¦¹¨BÆJ¦³¨âÓ³¡¥÷¡GY¨S¦³§ó·s¿ù»~¡A«h¸ê®Æ¨Ó·½·|¤ÏÀ³§ó·s¸ê®Æ¡C¥æ©ö³Q³\¥i¡C³\¥i¥æ©ö·|§¹¦¨©Mµ²§ô¥æ©ö¡C
±µ¨ü§ó·sªºµ{¦¡½X¦p¤U©Ò¥Ü¡G
cnn.CommitTrans
Yµo¥Í§ó·s¿ù»~¡A¥¦Ì·|³Q®·®»¨ì¿ù»~³B²z±`¦¡¤º¡C½Ð¥Î adFilterConflictingRecords ±`¼Æ¨Ó¹LÂo Recordset¡A¥uÅã¥Ü½Ä¬ðªº¸ê®Æ¦C¡C¿ù»~¸Ñ¨Mµ¦²¤¥u·|¦C¦L§@ªÌªº©m¦W
(au_fname ©M au_lname)¡AµM«á¦^´_ (ÁÙì) ¥æ©ö¡C¦^´_¥æ©ö·|±Ë±ó¥ô¦ó¦¨¥\ªº§ó·s§@·~¡AµM«áµ²§ô¥æ©ö¡C
©Úµ´§ó·sªºµ{¦¡½X¦p¤U©Ò¥Ü¡G
rst.Filter = adFilterConflictingRecords
rst.MoveFirst
Do While Not rst.EOF
Debug.Print "Conflict: Name = "; rst!au_fname; " "; rst!au_lname
rst.MoveNext
cnn.RollbackTrans
§ó·sµ²§ô«á¡ARecordset ©M Connection
ª«¥ó·|Ãö³¬¦Ó½d¨Ò·|¦s¦b¡Cµ{¦¡½X¦p¤U©Ò¥Ü¡G
rst.Close
cnn.Close
³o¬O»¡©ú©Ê±Ð¾Ç½Òµ{ªºµ²§À¡C