数学建模社区-数学中国

标题: 怎样用ADO打开一个带密码的Access库? [打印本页]

作者: 韩冰    时间: 2005-1-26 12:38
标题: 怎样用ADO打开一个带密码的Access库?
<>  </P>
7 x  o9 n- c+ {: \2 T7 E<>Creates a new Recordset object and appends it to the Recordsets collection. </P>( @0 R! L" A  y5 ]: C# v1 i: q; R
<>  </P>' x1 X9 j& T/ c  x
<>Syntax </P>
) v- X; ]; `. Y<>  </P>' I7 |8 b$ G3 k7 v/ h% S9 n
<>For Connection and Database objects: </P>
7 m) x. Q" R- @# l3 |- v# [4 O<>  </P>
" P6 Y6 G8 F/ L<>Set recordset = object.OpenRecordset (source, type, options, lockedits) </P>
$ a2 t1 I/ A( ]3 b* Y, r<>  </P>
6 _6 [5 ]1 @1 y9 h) y<>For QueryDef, Recordset, and TableDef objects: </P>/ K( B% J% @, B3 D0 _+ B9 }/ j
<>  </P>
0 b& Q6 V) j# Q  g<>Set recordset = object.OpenRecordset (type, options, lockedits) </P>$ u; M& G; {7 i( j% q: B
<>  </P>& d6 }7 b1 Q$ C  a4 c1 Q
<>The OpenRecordset method syntax has these parts. </P>
/ w- f3 I9 K' ]) k( e<>  </P>& O) o! `6 ]0 F' N0 Q) y% ?
<>art    Description </P>
* {# w: `% G* f1 c<>recordset       An object variable that represents the Recordset object you wantt to </P>% Y1 H  L$ [' D, ~1 u# J
<>open. </P>
3 u* p3 d) A2 E1 W' Q<>object  An object variable that represents an existing object from which you </P>
3 `* a( Y) a) z( g0 n<>want to create the new Recordset. </P>/ N1 A, A! \5 Q1 L
<>source  A String specifying the source of the records for the new Recordset. </P>
4 A  M+ C9 T1 O<>The source can be a table name, a query name, or an SQL statement that </P>$ M5 {" L9 z7 k% Y8 Q# [+ ?
<>returns records. For table-type Recordset objects in Microsoft Jet databases, </P>
5 v8 f3 b7 o- `: {<>the source can only be a table name. </P>: b2 q  v! r# a& ?3 K" W
<>type    Optional. A constant that indicates the type of Recordset to open, as </P>
3 ~3 T7 z. n4 J; q<>specified in Settings. </P>
; f3 G5 N2 s5 V  L# u4 D<>options Optional. A combination of constants that specify characteristics of </P># A3 @: ]) z& R$ N! A
<>the new Recordset, as listed in Settings. </P>, t5 t6 G; B" Y. @
<>lockedits       Optional. A constant that determines the locking for the Recordsset, </P>
4 h! N; e: u: R3 l<P>as specified in Settings. </P>  g5 S+ L$ g5 s  N5 K1 c
<P>Settings </P>
3 j  s1 D. l" \# ~* V- ]<P>  </P>
  z  [  h) k- y/ `7 K/ ]<P>You can use one of the following constants for the type argument. </P>
# `9 n5 m+ z; e2 n3 [+ d7 u<P>  </P>- N9 s2 g8 v" g% A& r
<P>Constant        Description </P>
4 \* M+ a- v; v9 K, {' t<P>  </P>
7 Z4 D% O0 T* \' l7 S2 v1 l<P>  </P>" Q) ]% d) m' q2 y
<P>dbOpenTable     Opens a table-type Recordset object (Microsoft Jet workspaces </P>
3 s- A/ l# M: l<P>only). </P>
  B% }9 R3 f& u6 }- S+ n1 C<P>dbOpenDynamic   Opens a dynamic-type Recordset object, which is similar to an </P>) Y1 u8 u2 O" a: {* D$ K$ ^
<P>ODBC dynamic cursor. (ODBCDirect workspaces only) </P>, k- ?: v1 N( D! u: t/ y
<P>dbOpenDynaset   Opens a dynaset-type Recordset object, which is similar to an </P>
8 M3 x. f: A6 m  F# Y  }<P>ODBC keyset cursor. </P>, m0 ^! a" V, e0 h1 o. ~
<P>dbOpenSnapshot  Opens a snapshot-type Recordset object, which is similar to an </P>
- g, o& C! x. Z: D5 ], m<P>ODBC static cursor. </P>
6 H5 P, M2 y9 r<P>dbOpenForwardOnly?Opens a forward-only-type Recordset object. </P>
8 k4 v, U$ S" O<P>Note   If you open a Recordset in a Microsoft Jet workspace and you don't </P>& F: n, W" E6 W
<P>specify a type, OpenRecordset creates a table-type Recordset, if possible. If </P>" i. y) x& F0 O! R- J
<P>you specify a linked table or query, OpenRecordset creates a dynaset-type </P>* @) \7 F/ w# Z/ H8 \+ K5 V: |' D  G" ]
<P>Recordset. In an ODBCDirect workspace, the default setting is dbOpenForwardOnl </P>4 _6 j9 }$ ]$ _. ?2 G; x2 z( G
<P>y. </P>9 ]( e* P4 g' j2 q$ ]3 g, l
<P>  </P>0 |* n1 L. t1 L! B' k. R$ ^/ K
<P>You can use a combination of the following constants for the options </P>$ |2 I; v( z- G5 _
<P>argument. </P>
. P) z$ D8 Q: T# G5 E+ z& m  O<P>  </P>! a2 B0 Z/ w  o( R) p
<P>Constant        Description </P>  h9 W* w& N- N7 x' g) O; I1 M
<P>dbAppendOnly?Allows users to append new records to the Recordset, but </P>
- ]/ {4 \. r( l# C<P>prevents them from editing or deleting existing records (Microsoft Jet </P>
' ?3 _1 R( ^+ \( [2 e% `<P>dynaset-type Recordset only). </P>
9 b# ^8 i; e. u, V<P>dbSQLPassThrough?Passes an SQL statement to a Microsoft Jet-connected ODBC </P>
* W9 P7 R1 @. h# Z<P>data source for processing (Microsoft Jet snapshot-type Recordset only). </P>/ p6 x2 o3 S; O$ B' X% H' L
<P>dbSeeChanges    Generates a run-time error if one user is changing data that </P>6 M1 b4 G4 O" @4 [+ U# O# A
<P>another user is editing (Microsoft Jet dynaset-type Recordset only). This is </P>9 Z, Y, P4 h" m! S7 c  P9 K  b
<P>useful in applications where multiple users have simultaneous read/write </P>
# Q! W4 b4 U; G& }2 b( `  r2 k<P>access to the same data. </P>3 Q" W1 w" R, T; l2 @' ^
<P>dbDenyWrite?Prevents other users from modifying or adding records (Microsoft </P>. o$ b  F0 [: o, z
<P>Jet Recordset objects only). </P>) z; j/ D' F  }4 X- C2 h
<P>dbDenyRead?Prevents other users from reading data in a table (Microsoft Jet </P>4 a* I/ x" J+ f4 T
<P>table-type Recordset only). </P>" y) I/ e, }& O% `$ B; }
<P>dbForwardOnly?Creates a forward-only Recordset (Microsoft Jet snapshot-type </P>
" P2 k8 D. P$ \' {5 t<P>Recordset only). It is provided only for backward compatibility, and you </P>2 i1 o2 ?. `, Z4 p8 l; {" }
<P>should use the dbOpenForwardOnly constant in the type argument instead of </P>( Z7 B0 g$ s8 w! }* m3 o
<P>using this option. </P>) V* @$ O' d8 k2 |- h% ?
<P>dbReadOnly?Prevents users from making changes to the Recordset (Microsoft Jet </P>
& r- C+ b9 N$ F, v" s<P>only). The dbReadOnly constant in the lockedits argument replaces this </P># `' }: q4 Y( @4 q: d% J/ a
<P>option, which is provided only for backward compatibility. </P>
4 Q% R; k0 n2 Y2 Y<P>dbRunAsync      Runs an asynchronous query (ODBCDirect workspaces only). </P>$ R( z+ I9 o9 p$ M
<P>dbExecDirect?Runs a query by skipping SQLPrepare and directly calling </P>
& ^8 M4 S+ L$ K& N- A<P>SQLExecDirect (ODBCDirect workspaces only). Use this option only when you抮e </P>
: J5 b1 Z5 d: Y/ J' K. ?. o<P>not opening a Recordset based on a parameter query. For more information, see </P>- j$ G. V/ _8 s4 }/ E1 N5 e
<P>the "Microsoft ODBC 3.0 Programmer抯 Reference." </P>
$ R+ t2 E( T+ f: ]0 r: w% A9 g<P>dbInconsistent?Allows inconsistent updates (Microsoft Jet dynaset-type and </P>  F' C4 z8 E# e5 A, r
<P>snapshot-type Recordset objects only). </P>/ {$ a3 Z# F/ {& ?2 Z* n
<P>dbConsistent?Allows only consistent updates (Microsoft Jet dynaset-type and </P>
: z+ ~/ B: u8 d, O3 D<P>snapshot-type Recordset objects only). </P>
; [2 b  J3 V( f, \<P>Note   The constants dbConsistent and dbInconsistent are mutually exclusive, </P>) X. |9 m6 \, B4 x# p. x
<P>and using both causes an error. Supplying a lockedits argument when options </P>6 @" d) p+ F; ]) e! b
<P>uses the dbReadOnly constant also causes an error. </P># S' G* O, _6 `. L9 u0 r( K% i
<P>  </P>
. K9 S) E, l) L6 E<P>You can use the following constants for the lockedits argument. </P>
+ z- U! M' B1 e9 F# z1 D<P>  </P>6 _' P3 h. Z- L
<P>Constant        Description </P>" H6 z* k2 U7 S& b, e. b
<P>dbReadOnly      Prevents users from making changes to the Recordset (default for </P>" V% A; a' H1 e" J8 S1 Q
<P>ODBCDirect workspaces). You can use dbReadOnly in either the options argument </P>7 f5 d/ }5 j/ v- P" w9 r! m* j
<P>or the lockedits argument, but not both. If you use it for both arguments, a </P>, N8 e" B( O/ |9 t: }1 b' b7 f
<P>run-time error occurs. </P>: |! w! I' f; y2 ~: T
<P>dbPessimistic?Uses pessimistic locking to determine how changes are made to </P>/ G% Y* r7 b/ e* ]7 b0 ^
<P>the Recordset in a multiuser environment. The page containing the record </P>2 b- ^! S; A" Y7 u% O- A
<P>you're editing is locked as soon as you use the Edit method (default for </P>
" C0 n, j( L2 W" F/ n/ R+ W% |<P>Microsoft Jet workspaces). </P>" f5 j" h% c( V$ i5 h1 [; N
<P>dbOptimistic?Uses optimistic locking to determine how changes are made to the </P>
9 m: w  n- a1 k<P>Recordset in a multiuser environment. The page containing the record is not </P>
- }4 i7 q; w& p, e" }<P>locked until the Update method is executed. </P>3 H2 B! i) E" A& B
<P>dbOptimisticValue?Uses optimistic concurrency based on row values (ODBCDirect </P>
3 b1 G  \) N7 M9 r<P>workspaces only). </P>
9 f, h9 q- C' J; s. M<P>dbOptimisticBatch?Enables batch optimistic updating (ODBCDirect workspaces </P>
& F! e: I9 M! R: W2 I<P>only). </P>
7 F# q1 T& t$ h# E5 P<P>Remarks </P>7 F: `  p: A+ k1 G  k
<P>  </P>
+ G, g/ Z- t2 t# r! k<P>In a Microsoft Jet workspace, if object refers to a QueryDef object, or a </P>% Y8 J. c& m, n8 S( h
<P>dynaset- or snapshot-type Recordset, or if source refers to an SQL statement </P>
2 a6 t% R# o+ Z" v3 ~( q<P>or a TableDef that represents a linked table, you can't use dbOpenTable for </P>
3 t" a# x' {$ Z, n/ r  R0 c4 q<P>the type argument; if you do, a run-time error occurs. If you want to use an </P>
! W9 I/ }: ?7 E7 _4 Z  ~- {$ \9 o<P>SQL pass-through query on a linked table in a Microsoft Jet-connected ODBC </P>
0 h- {9 s8 @8 j1 W6 O<P>data source, you must first set the Connect property of the linked table's </P>
7 p( w5 o3 Z9 o- X' [<P>database to a valid ODBC connection string. If you only need to make a single </P>
9 |& _9 j" `: I7 s# I7 V/ n<P>pass through a Recordset opened from a Microsoft Jet-connected ODBC data </P>) [+ V3 l, Z3 h# _
<P>source, you can improve performance by using dbOpenForwardOnly for the type </P>
! r& P; h% U, H' o# U# Q<P>argument. </P>4 Z& ^) [0 e7 Y! z  q1 g
<P>  </P>5 u- O4 K7 @+ h5 p
<P>If object refers to a dynaset- or snapshot-type Recordset, the new Recordset </P>
/ E6 @- x9 E5 n  Q) K- r. {' ~4 G<P>is of the same type object. If object </P>) U, v5 H2 t" L1 H
<P> refers to a table-type Recordset object, the type of the new object is a </P>
; X$ \8 ]) [4 V1 h% G<P>dynaset-type Recordset. You can't open new Recordset objects from forward-only </P>4 a4 t1 R$ @2 E# c
<P>杢ype or ODBCDirect Recordset objects. </P>
8 A- u( h. B* c<P>In an ODBCDirect workspace, you can open a Recordset containing more than one </P>
& C8 B5 l7 _6 M# V+ S/ j$ ^<P>select query in the source argument, such as </P>/ [  E" |, g3 G( s9 F  }) d
<P>  </P>. V  P% \1 A, b: K5 g0 y0 w0 E
<P>"SELECT LastName, FirstName FROM Authors </P>1 p$ h; J2 @6 n3 ^7 P
<P>WHERE LastName = 'Smith'; </P>
$ y) P/ ?3 k0 H; o% t- g7 k. `<P>SELECT Title, ISBN FROM Titles </P>1 @# \2 ^" H- n0 @0 @, ~* `! C0 O
<P>WHERE ISBN Like '1-55615-*'" </P>3 G( o3 c6 @8 C3 ?4 M2 Y3 j
<P>  </P>6 v) `# |1 m- P
<P>The returned Recordset will open with the results of the first query. To </P>
- ^# N9 V6 o9 s/ ?7 n+ Z8 [<P>obtain the result sets of records from subsequent queries, use the </P>& u+ {9 F7 r% @. z+ n9 l) y
<P>NextRecordset method. </P>9 J1 u6 E1 E% j
<P>  </P>
6 c) ?; h$ {/ k& P0 f<P>Note   You can send DAO queries to a variety of different database servers </P>" ~6 Y% q- S( {4 f( c" B
<P>with ODBCDirect, and different servers will recognize slightly different </P>/ T2 T' K: h6 f+ p
<P>dialects of SQL. Therefore, context-sensitive Help is no longer provided for </P>
$ J) d  ]- q6 R<P>Microsoft Jet SQL, although online Help for Microsoft Jet SQL is still </P>
) T# B$ I! H8 G9 V4 |<P>included through the Help menu. Be sure to check the appropriate reference </P>
! A5 J+ U4 o5 F3 v<P>documentation for the SQL dialect of your database server when using either </P>
3 i, g! L2 ]! P- _; g- ~6 z, ^+ l5 U<P>ODBCDirect connections or pass-through queries in Microsoft Jet-connected </P>( F- r  F) r" c* }1 K, |
<P>client/server applications. </P>
4 o9 c+ A# C+ ?4 ]<P>  </P>8 A8 u: U; K6 t3 f7 S5 Y6 F
<P>Use the dbSeeChanges constant in a Microsoft Jet workspace if you want to </P>
6 E- D4 l( p) r6 K5 n/ \<P>trap changes while two or more users are editing or deleting the same record. </P>
. W$ N+ N3 h# Y- Q) V/ h<P>For example, if two users start editing the same record, the first user to </P>
7 L' m0 Z- Y  H( t9 h" M  f<P>execute the Update method succeeds. When the second user invokes the Update </P>
. p0 e! m; `; a: m$ C<P>method, a run-time error occurs. Similarly, if the second user tries to use </P>7 g# Y9 @1 v% [3 q6 X$ k
<P>the Delete method to delete the record, and the first user has already </P>
  k  B& }1 T1 V! J5 P<P>changed it, a run-time error occurs. </P>
# s6 N1 {: z2 w- E* Q<P>  </P>! R* H$ b! U) i4 P3 S+ p
<P>Typically, if the user gets this error while updating a record, your code </P>/ O* Y1 c1 I- b% l. P
<P>should refresh the contents of the fields and retrieve the newly modified </P>
6 S( c+ {% [$ E, ?. k+ E( N<P>values. If the error occurs while deleting a record, your code could display </P>7 \# G2 |9 {% d: p- ?- J  ~
<P>the new record data to the user and a message indicating that the data has </P>% I  _6 o6 z( R9 n* Z) p
<P>recently changed. At this point, your code can request a confirmation that </P>
! [' a( S+ G6 ^, J3 M& }<P>the user still wants to delete the record. </P>0 @" O+ j6 T$ U+ V  W
<P>  </P>
% |4 a) _: {( D- }; k- j9 n3 h2 k3 ?<P>You should also use the dbSeeChanges constant if you open a Recordset in a </P>0 L: v- _+ M: L: ]+ s0 @
<P>Microsoft Jet-connected ODBC workspace against a Microsoft SQL Server 6.0 (or </P>. \& O% ?2 ], N. K6 R* _* l
<P>later) table that has an IDENTITY column, otherwise an error may result. </P>6 J& e. n5 [% U: C' y3 n: X/ g
<P>  </P>6 t( |7 W1 K# r, t
<P>In an ODBCDirect workspace, you can execute asynchronous queries by setting </P>0 X4 d- d; Q7 `9 @3 U  Y( k
<P>the dbRunAsync constant in the options argument. This allows your application </P>
& F" n! P9 ]2 R' @<P>to continue processing other statements while the query runs in the </P>3 g) w9 J0 `- m9 Z4 r) L( B
<P>background. But, you cannot access the Recordset data until the query has </P>
3 U: B. ?* Q& n7 g- ]<P>completed. To determine whether the query has finished executing, check the </P>
' S5 Y2 V) k& _* ~; ?  N<P>StillExecuting property of the new Recordset. If the query takes longer to </P>5 k. b, K8 b3 D1 a2 `& V# l9 h
<P>complete than you anticipated, you can terminate execution of the query with </P>( t) ^; K6 J- e$ n4 Q
<P>the Cancel method. </P>
) T2 h9 h( u% C" I<P>  </P>: o! _& B1 R/ y0 Y( ?9 ^# j
<P>Opening more than one Recordset on an ODBC data source may fail because the </P>
; e$ {3 A- _' O8 Y$ {. S<P>connection is busy with a prior </P>
: O' ~: T0 m" w: V<P>OpenRecordset call. One way around this is to use a server-side cursor and </P>
8 ~( }$ y) b/ d. l% g. F" d<P>ODBCDirect, if the server supports this. Another solution is to fully </P>
; F2 O% r* A4 i, X' m/ Y" i<P>populate the Recordset by using the MoveLast method as soon as the Recordset </P>+ J% I4 h2 E1 |3 X
<P>is opened. </P>5 O% [3 C. W& Z6 d
<P>  </P>
8 _3 H' l3 f' A) x9 O# I<P>If you open a Connection object with DefaultCursorDriver set to </P>
+ X" b- l. Y# c0 m! O<P>dbUseClientBatchCursor, you can open a Recordset to cache changes to the data </P>
0 e% y: f9 s) t" U  M<P>(known as batch updating) in an ODBCDirect workspace. Include dbOptimisticBatc </P># u& s3 L0 D' K: i
<P>h in the lockedits argument to enable update caching. See the Update method </P>( N9 s; @/ Z9 x
<P>topic for details about how to write changes to disk immediately, or to cache </P>
& i5 x! j' I% r7 z<P>changes and write them to disk as a batch. </P>/ g& z/ T. V$ V. I, g. Q
<P>  </P>
! s. {. }' L( V8 \! S8 L<P>Closing a Recordset with the Close method automatically deletes it from the </P>( n5 ^1 e) T# t% i/ N

4 L+ f7 H6 z# `2 |& T<P>Recordsets collection. </P>
' y  i& }, {& n* l2 M3 p4 Y<P>  </P>
3 y/ F. f, M% s9 @; [9 k5 P<P>Note   If source refers to an SQL statement composed of a string concatenated </P>
& l: w: T: A1 ]3 a9 N<P>with a non-integer value, and the system parameters specify a non-U.S. </P>
& S+ @+ f+ V7 P' ~7 ^! a<P>decimal character such as a comma (for example, strSQL = "PRICE &gt; " &amp; </P>
4 J) _1 r0 U2 w1 d) S" Z2 t<P>lngPrice, and lngPrice = 125,50), an error occurs when you try to open the </P>7 T' q$ i# {; s  H' I1 q
<P>Recordset. This is because during concatenation, the number will be converted </P>
6 u" B" E" N; n3 b; h+ I1 V<P>to a string using your system's default decimal character, and SQL only </P>
0 z$ `2 w9 G9 [3 b1 h5 K. h! [<P>accepts U.S. decimal characters.</P>




欢迎光临 数学建模社区-数学中国 (http://www.madio.net/) Powered by Discuz! X2.5