数学建模社区-数学中国

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

作者: 韩冰    时间: 2005-1-26 12:38
标题: 怎样用ADO打开一个带密码的Access库?
<>  </P>3 k* G- H( z/ G6 C4 V
<>Creates a new Recordset object and appends it to the Recordsets collection. </P>2 L' k$ ]5 Q: m& x! P% ^; v
<>  </P>
+ Z9 o% _& d* T& N$ ]<>Syntax </P>& }5 I1 t* `/ Q
<>  </P>
8 a: B: `! ?. i3 Z( D; M# i7 s<>For Connection and Database objects: </P>3 e1 e, X$ X$ ]; F! }4 G5 a/ v6 i
<>  </P>: U# t% S6 N" W2 D6 D# a4 r6 R
<>Set recordset = object.OpenRecordset (source, type, options, lockedits) </P>
$ g' r. E$ A9 @& Q7 p8 L+ s<>  </P>
& |( U6 B+ G; m- C- x<>For QueryDef, Recordset, and TableDef objects: </P>
. Z: O& U3 k. T$ w<>  </P>5 A' T9 S" }' V. _
<>Set recordset = object.OpenRecordset (type, options, lockedits) </P>3 x) c* C. O! M9 r7 p
<>  </P>( O; V. V& M1 l0 `
<>The OpenRecordset method syntax has these parts. </P>  I9 y- f8 y. W+ S" v- ^5 r# c
<>  </P>
+ b9 a( {3 E' Q3 \8 K9 v* {<>art    Description </P>! x( E0 Y; K8 @% L( l/ I
<>recordset       An object variable that represents the Recordset object you wantt to </P>
2 W( z$ E6 q: x- @% y<>open. </P>0 c! i- r* W0 u* q9 V# C& v% F
<>object  An object variable that represents an existing object from which you </P>+ z6 G! I" q9 k8 m
<>want to create the new Recordset. </P>1 Y5 L, c3 `  J$ [8 D
<>source  A String specifying the source of the records for the new Recordset. </P>
) u3 g5 K8 B" N<>The source can be a table name, a query name, or an SQL statement that </P>  t8 K" j2 r; V
<>returns records. For table-type Recordset objects in Microsoft Jet databases, </P>
: b/ F4 F' C/ q* Y2 p<>the source can only be a table name. </P>
6 g5 e6 R( g  L$ l  P: {<>type    Optional. A constant that indicates the type of Recordset to open, as </P>
1 t+ K6 F$ k5 m7 m9 J<>specified in Settings. </P>3 Y1 ?' [$ Z& L; {4 K! P6 ^9 o& J
<>options Optional. A combination of constants that specify characteristics of </P>: u, J! H# B: q- `4 |
<>the new Recordset, as listed in Settings. </P>
# H/ Q. w1 N* F' ~0 G: m. R<>lockedits       Optional. A constant that determines the locking for the Recordsset, </P>
5 F5 c9 k" \- S7 `* o' k% \<P>as specified in Settings. </P>
2 ^; R2 R4 r0 t7 U+ _, h<P>Settings </P>! Z8 d% o; e! ]7 ~( m
<P>  </P>3 D  H( ^$ H1 I% @
<P>You can use one of the following constants for the type argument. </P>
2 d0 _; ^2 I" O( j) G- f: M& a<P>  </P>
" }: z6 C/ o- g) r<P>Constant        Description </P>4 l) ]! G' D1 F( Z2 d9 _
<P>  </P>
& T& T8 R/ U) k; M$ V<P>  </P>+ a! [# j$ {2 U$ B+ f
<P>dbOpenTable     Opens a table-type Recordset object (Microsoft Jet workspaces </P>
" x* w) j' q. `( K) ^1 e<P>only). </P>
1 U  F' o" u4 t# b<P>dbOpenDynamic   Opens a dynamic-type Recordset object, which is similar to an </P>3 a' L. G6 s5 P4 E* _" B; A6 T
<P>ODBC dynamic cursor. (ODBCDirect workspaces only) </P>! `! o9 w& F8 B) P$ q2 p' ~
<P>dbOpenDynaset   Opens a dynaset-type Recordset object, which is similar to an </P>( D5 k9 D  R  o' D
<P>ODBC keyset cursor. </P>( v( h1 Q+ I+ C1 g4 B
<P>dbOpenSnapshot  Opens a snapshot-type Recordset object, which is similar to an </P>
4 K4 {* a: N# H3 x5 w1 r* W<P>ODBC static cursor. </P>' r. Y1 \* x# B5 j
<P>dbOpenForwardOnly?Opens a forward-only-type Recordset object. </P>' `) x3 ]+ R/ a( W, }! \) t2 ]$ j
<P>Note   If you open a Recordset in a Microsoft Jet workspace and you don't </P>6 W1 W0 k! U% n2 J# [1 L5 f2 [6 Y
<P>specify a type, OpenRecordset creates a table-type Recordset, if possible. If </P>5 ~1 S- A: b2 C1 n& E7 I
<P>you specify a linked table or query, OpenRecordset creates a dynaset-type </P>
1 c& x1 _& C  {% p2 p" J9 N<P>Recordset. In an ODBCDirect workspace, the default setting is dbOpenForwardOnl </P># }& C4 C6 X  J5 J" m  \
<P>y. </P>% T# {$ K" H) g1 p: D
<P>  </P>+ q& j. z' U9 O9 b, ]
<P>You can use a combination of the following constants for the options </P>
+ b: [1 m1 }9 Y5 e+ `<P>argument. </P>/ [" l% p. i" b! N; l) @
<P>  </P># G; o7 ?) b3 A. c+ P% K5 a
<P>Constant        Description </P>0 v& T# w! Q0 y( g, n. G9 B& @
<P>dbAppendOnly?Allows users to append new records to the Recordset, but </P>) q: t2 D; {1 g2 g& E/ C
<P>prevents them from editing or deleting existing records (Microsoft Jet </P>  [4 T2 r5 u6 v5 M/ u
<P>dynaset-type Recordset only). </P>0 c4 x& c$ q0 |* f
<P>dbSQLPassThrough?Passes an SQL statement to a Microsoft Jet-connected ODBC </P>
- t3 w0 l& w/ B; G% R<P>data source for processing (Microsoft Jet snapshot-type Recordset only). </P>9 l; b6 s5 c; p$ T
<P>dbSeeChanges    Generates a run-time error if one user is changing data that </P>
# H9 i5 {, Y& e( b<P>another user is editing (Microsoft Jet dynaset-type Recordset only). This is </P>4 k, b3 ?. o- k1 k4 n
<P>useful in applications where multiple users have simultaneous read/write </P>
7 \3 l8 N+ U+ C% C8 k5 O, p<P>access to the same data. </P>1 d. L- u; T4 o+ w. r( }% D7 p
<P>dbDenyWrite?Prevents other users from modifying or adding records (Microsoft </P>3 P8 d/ n  Y1 U8 o; H$ f$ G
<P>Jet Recordset objects only). </P>! o4 a/ m* K7 y& f
<P>dbDenyRead?Prevents other users from reading data in a table (Microsoft Jet </P>& R1 D- f5 ?+ p1 o# L
<P>table-type Recordset only). </P>& k9 {) t0 q4 n: y" c& U: a) N
<P>dbForwardOnly?Creates a forward-only Recordset (Microsoft Jet snapshot-type </P>/ |8 a2 m# l) ^" `
<P>Recordset only). It is provided only for backward compatibility, and you </P>
& e9 {7 x% }. b/ A<P>should use the dbOpenForwardOnly constant in the type argument instead of </P>4 e* P+ }) c, k  V9 d
<P>using this option. </P>
& z+ I2 |" Z& D: N& B3 n<P>dbReadOnly?Prevents users from making changes to the Recordset (Microsoft Jet </P>9 x, D: g" c) _1 I9 z* ^
<P>only). The dbReadOnly constant in the lockedits argument replaces this </P>6 l$ o2 b/ E+ c( k( u: }- D; N+ ~
<P>option, which is provided only for backward compatibility. </P>
/ S$ G$ n6 y1 Q+ ?3 L6 O9 Z<P>dbRunAsync      Runs an asynchronous query (ODBCDirect workspaces only). </P>& t' J; t  q( x# [& e) v
<P>dbExecDirect?Runs a query by skipping SQLPrepare and directly calling </P>; C# b; o0 ^2 E& m
<P>SQLExecDirect (ODBCDirect workspaces only). Use this option only when you抮e </P>, M6 L: p* F8 W5 z+ G9 t, O
<P>not opening a Recordset based on a parameter query. For more information, see </P>
( @" w# I5 b7 N2 ?$ L: d# v$ E& @<P>the "Microsoft ODBC 3.0 Programmer抯 Reference." </P>  {; `5 C9 C7 Y
<P>dbInconsistent?Allows inconsistent updates (Microsoft Jet dynaset-type and </P>
: |' a6 N; W; B& a<P>snapshot-type Recordset objects only). </P>
( P1 c4 e) |+ r% ^4 Y2 {<P>dbConsistent?Allows only consistent updates (Microsoft Jet dynaset-type and </P>* o* e/ t( D$ b# i
<P>snapshot-type Recordset objects only). </P>5 n  |4 Q% D- e+ y
<P>Note   The constants dbConsistent and dbInconsistent are mutually exclusive, </P>
7 H% q, C+ ~9 S% e0 H! p<P>and using both causes an error. Supplying a lockedits argument when options </P>7 g; S. r; x7 T2 F
<P>uses the dbReadOnly constant also causes an error. </P>
. S6 V1 p) U2 M4 V2 U6 J6 V/ d7 }<P>  </P>0 [& Y. R9 [2 ^& x: R& e
<P>You can use the following constants for the lockedits argument. </P>
& n) J1 S6 Q( k<P>  </P>
1 W8 u$ r' P2 U<P>Constant        Description </P>
/ U$ f& h/ M3 i) `7 A<P>dbReadOnly      Prevents users from making changes to the Recordset (default for </P>& F- n+ [  A5 C* L* ?. @
<P>ODBCDirect workspaces). You can use dbReadOnly in either the options argument </P>
$ G- ?/ k: B0 c" _) B1 g- U<P>or the lockedits argument, but not both. If you use it for both arguments, a </P>
1 Y% u/ H; a6 s7 L<P>run-time error occurs. </P>- N9 m5 ?1 J. Q) {7 v1 S
<P>dbPessimistic?Uses pessimistic locking to determine how changes are made to </P>
7 E! n; S( q  c4 y$ o. B3 u7 c( w2 C- m) V<P>the Recordset in a multiuser environment. The page containing the record </P>, D# R! n1 ~0 q. ]7 n# m  J
<P>you're editing is locked as soon as you use the Edit method (default for </P>
- n# u6 t% _% ?4 R<P>Microsoft Jet workspaces). </P>
& L: A4 p% Z! N, t4 \<P>dbOptimistic?Uses optimistic locking to determine how changes are made to the </P>
" a9 d" Y: A; F1 b<P>Recordset in a multiuser environment. The page containing the record is not </P>1 u1 t. }8 |/ j0 m- X4 D3 H( u
<P>locked until the Update method is executed. </P>
! T2 Z2 V8 u7 X" N, I( |<P>dbOptimisticValue?Uses optimistic concurrency based on row values (ODBCDirect </P>  c2 y" d$ d- h' d
<P>workspaces only). </P>% E, W2 S. L+ x
<P>dbOptimisticBatch?Enables batch optimistic updating (ODBCDirect workspaces </P>3 Q8 H0 L+ \/ n5 o- ~
<P>only). </P>; R5 w4 o8 o. D; }8 c3 u
<P>Remarks </P>
9 S6 V3 w! p7 O8 ^4 b0 U<P>  </P>
4 V, a4 S. ~( W. z: P! U2 n<P>In a Microsoft Jet workspace, if object refers to a QueryDef object, or a </P>
( m9 w, j$ s6 v5 G3 }$ A<P>dynaset- or snapshot-type Recordset, or if source refers to an SQL statement </P>
! C# w2 k% [( r% q. ~<P>or a TableDef that represents a linked table, you can't use dbOpenTable for </P>6 c$ l9 s) i" ~5 h+ f
<P>the type argument; if you do, a run-time error occurs. If you want to use an </P>
+ p0 j% u! }: _0 L# n- @+ Z- O<P>SQL pass-through query on a linked table in a Microsoft Jet-connected ODBC </P>
. j4 K3 b6 z+ L$ l<P>data source, you must first set the Connect property of the linked table's </P>& c+ o9 w4 s" K+ X2 x- s* A% _  t
<P>database to a valid ODBC connection string. If you only need to make a single </P>0 r7 b, h2 d5 C5 p$ J
<P>pass through a Recordset opened from a Microsoft Jet-connected ODBC data </P>" X8 P* S; X, b. U  ^, S, B: h
<P>source, you can improve performance by using dbOpenForwardOnly for the type </P>6 _/ a4 O' K+ ^+ E/ q/ J7 |" q/ H0 {
<P>argument. </P>' u! r4 S. K8 `1 y4 t
<P>  </P>
8 g( m( r2 Y( R<P>If object refers to a dynaset- or snapshot-type Recordset, the new Recordset </P>+ G# [8 \; s/ Q
<P>is of the same type object. If object </P>) w- P! j8 L8 j
<P> refers to a table-type Recordset object, the type of the new object is a </P>
  f" X  r# b3 _& q- D; x<P>dynaset-type Recordset. You can't open new Recordset objects from forward-only </P>
. m0 w9 C9 r! Q* }<P>杢ype or ODBCDirect Recordset objects. </P>9 S; Q- B" o6 G0 p2 b
<P>In an ODBCDirect workspace, you can open a Recordset containing more than one </P>
4 u. _) M1 a5 j, I4 H<P>select query in the source argument, such as </P>0 K+ C( h5 @) N8 n6 I4 s
<P>  </P>
/ ]6 v/ `$ Z2 Z) ?% ]/ Z<P>"SELECT LastName, FirstName FROM Authors </P>% ]* ~7 ^  E. u
<P>WHERE LastName = 'Smith'; </P>$ {# r  v# r5 l/ _
<P>SELECT Title, ISBN FROM Titles </P>
7 Y' U3 J6 m" X) a' K. B% k2 d2 j<P>WHERE ISBN Like '1-55615-*'" </P>, L" u. [+ }* Z+ @9 n4 ]
<P>  </P>
* R5 p2 z! }0 O9 J<P>The returned Recordset will open with the results of the first query. To </P>
& s$ S2 P0 }1 I0 F<P>obtain the result sets of records from subsequent queries, use the </P># I# i1 x  x( x% [
<P>NextRecordset method. </P>: h/ p0 c0 B9 d' w1 g' c) j
<P>  </P>
" q- ?) n6 p8 F0 `8 d) V<P>Note   You can send DAO queries to a variety of different database servers </P>7 ]$ ]/ ~1 b/ V1 q8 M& f6 t
<P>with ODBCDirect, and different servers will recognize slightly different </P>
- ~0 R% \" `3 p/ L( x<P>dialects of SQL. Therefore, context-sensitive Help is no longer provided for </P>
6 w, d  w* U* y/ a% K<P>Microsoft Jet SQL, although online Help for Microsoft Jet SQL is still </P>
$ h4 Y9 O* C- E# d6 f<P>included through the Help menu. Be sure to check the appropriate reference </P>
, [: x1 W' Z9 z% b0 t<P>documentation for the SQL dialect of your database server when using either </P>
  h  `7 U) l& h) F2 b7 e<P>ODBCDirect connections or pass-through queries in Microsoft Jet-connected </P>2 }# I" [; k- H
<P>client/server applications. </P>+ Z) O: X6 u+ o! R
<P>  </P>
! @' G- a: i- X7 b  K7 [<P>Use the dbSeeChanges constant in a Microsoft Jet workspace if you want to </P>
$ i3 K6 B  `; Y<P>trap changes while two or more users are editing or deleting the same record. </P>8 s) l2 u% S, X; ^: E
<P>For example, if two users start editing the same record, the first user to </P>
2 e' h/ C4 }7 R0 L. W0 K4 A  U<P>execute the Update method succeeds. When the second user invokes the Update </P>
) _" i) z/ `2 a( g0 @* z1 E; X<P>method, a run-time error occurs. Similarly, if the second user tries to use </P>
* w& B& K# X* L& S<P>the Delete method to delete the record, and the first user has already </P>5 s$ C7 e! }: J2 o9 n8 T' F
<P>changed it, a run-time error occurs. </P>
3 O3 u' W0 ^( \, z" Z0 T<P>  </P>
* P! `" {( E5 [; A<P>Typically, if the user gets this error while updating a record, your code </P>8 F3 B0 Q. j' O1 A* C9 \. `4 }
<P>should refresh the contents of the fields and retrieve the newly modified </P>
0 C* ~# m5 V/ h; W: W3 R8 [3 e<P>values. If the error occurs while deleting a record, your code could display </P>5 E4 b7 e; M. I- z
<P>the new record data to the user and a message indicating that the data has </P>2 a6 n# }6 K2 z% s3 b9 S* l
<P>recently changed. At this point, your code can request a confirmation that </P>  D5 K" n$ `5 V$ q+ i: G/ Z$ F
<P>the user still wants to delete the record. </P># P7 q4 ?. z$ ~2 M' L: d) ~
<P>  </P>- `5 E3 |6 l5 u2 w6 `) ~% Q6 k
<P>You should also use the dbSeeChanges constant if you open a Recordset in a </P>4 p7 Y4 X, i3 u* U
<P>Microsoft Jet-connected ODBC workspace against a Microsoft SQL Server 6.0 (or </P>
+ i' E7 @. j+ I; V7 p<P>later) table that has an IDENTITY column, otherwise an error may result. </P>
$ Q8 O. H$ n. N0 E! L9 p<P>  </P>( N6 D* Y8 F* D& w- ~9 j7 t* Z& O
<P>In an ODBCDirect workspace, you can execute asynchronous queries by setting </P>
% a; P$ [5 W. K% H<P>the dbRunAsync constant in the options argument. This allows your application </P>( ]& A- f6 q2 j" H
<P>to continue processing other statements while the query runs in the </P>' h" x1 C, E" M
<P>background. But, you cannot access the Recordset data until the query has </P>
. J: m( |% p( `) C<P>completed. To determine whether the query has finished executing, check the </P>
4 \4 y9 m# n0 j+ H6 `* T<P>StillExecuting property of the new Recordset. If the query takes longer to </P>, m1 X- r% A4 J" q6 W4 @: H; ^
<P>complete than you anticipated, you can terminate execution of the query with </P>
7 r/ H$ `% e0 f& C5 l, p( T& U<P>the Cancel method. </P>( ^8 x( ^/ W6 I1 m1 S
<P>  </P>8 b4 T& M6 I7 u
<P>Opening more than one Recordset on an ODBC data source may fail because the </P># N( X1 K; Q! V2 @
<P>connection is busy with a prior </P>
* Y$ B! j. I4 R% f5 C<P>OpenRecordset call. One way around this is to use a server-side cursor and </P>! l$ R) u/ L% o
<P>ODBCDirect, if the server supports this. Another solution is to fully </P>
7 F; B8 q+ x2 u4 i% f<P>populate the Recordset by using the MoveLast method as soon as the Recordset </P>
" X1 A( a- j- W* A8 ~9 x% W<P>is opened. </P>
( S) R( \3 d  L6 d3 Y3 G( W2 \% t<P>  </P>
' M5 e& B6 Y( M6 B7 O6 |<P>If you open a Connection object with DefaultCursorDriver set to </P>
+ x+ C: o$ [) ?4 Z; G1 a7 k: T<P>dbUseClientBatchCursor, you can open a Recordset to cache changes to the data </P>
1 x4 M( X& A1 Z6 F' x<P>(known as batch updating) in an ODBCDirect workspace. Include dbOptimisticBatc </P>
/ P& g/ x! F) @7 x<P>h in the lockedits argument to enable update caching. See the Update method </P>4 J2 w' G; ^2 A
<P>topic for details about how to write changes to disk immediately, or to cache </P>
, e7 r9 u) s4 p6 k9 R3 P<P>changes and write them to disk as a batch. </P>% ~2 x& c# b( k" Z  t6 x! ]
<P>  </P>
8 d5 F; o3 ^/ i: T: @( b2 Q0 U<P>Closing a Recordset with the Close method automatically deletes it from the </P>
4 U* q; |% X  d* ]- H& b' g8 U
8 v3 |5 Q% ~( D" H. ~/ E" i<P>Recordsets collection. </P>
- C( m, G! a2 `" q; ]2 b3 e6 m: I<P>  </P>, e8 U. r, s- `1 w; [+ P
<P>Note   If source refers to an SQL statement composed of a string concatenated </P>
0 v. a! P/ Q+ W3 N( j0 U<P>with a non-integer value, and the system parameters specify a non-U.S. </P>) g' \( D$ x# \4 f; d' m* p/ N
<P>decimal character such as a comma (for example, strSQL = "PRICE &gt; " &amp; </P>( v- n8 `' t- u9 ?) E
<P>lngPrice, and lngPrice = 125,50), an error occurs when you try to open the </P>  Q) p9 I9 t; x2 @9 e( v" R/ u
<P>Recordset. This is because during concatenation, the number will be converted </P>9 G" s5 q5 x& c( `/ A2 G- _% h
<P>to a string using your system's default decimal character, and SQL only </P>
9 z6 Z3 u# f% M. C/ a* C<P>accepts U.S. decimal characters.</P>




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