数学建模社区-数学中国

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

作者: 韩冰    时间: 2005-1-26 12:38
标题: 怎样用ADO打开一个带密码的Access库?
<>  </P>/ j4 R1 Q1 w( W& J: U5 a
<>Creates a new Recordset object and appends it to the Recordsets collection. </P>
' ^4 V; U/ F. L* c/ z<>  </P>$ h4 |& Q  j8 B0 \; i5 K  z
<>Syntax </P>5 v. Q+ n& F  X* A
<>  </P>. u4 a+ \0 I2 M2 j' \+ \
<>For Connection and Database objects: </P>
% t  I6 v; F- G& n9 `. Y<>  </P>6 A( G4 R, Z; Q1 D* O
<>Set recordset = object.OpenRecordset (source, type, options, lockedits) </P>) o; g/ n" Q0 b  [, u8 X! H6 }
<>  </P>
8 g& [* ]3 x, X" }: ^" H& a+ `<>For QueryDef, Recordset, and TableDef objects: </P>
- u9 V! z) f4 f<>  </P>9 F$ B- A% w* }1 b
<>Set recordset = object.OpenRecordset (type, options, lockedits) </P>/ b& o2 d. `( m1 v+ ]
<>  </P>
! [5 F" r# d$ q) O" k6 x4 Y<>The OpenRecordset method syntax has these parts. </P>
2 J; H' o6 f) p( `" W<>  </P>% P8 `" o1 t7 ^5 ?$ P9 g
<>art    Description </P>
4 }% c0 P  L- [9 @0 p' c<>recordset       An object variable that represents the Recordset object you wantt to </P>$ p" H, x( N6 V8 X  A& ]9 A4 `  }1 W
<>open. </P>
- I; H4 `8 H* c2 l2 H/ Y<>object  An object variable that represents an existing object from which you </P>
. D) w" D* x+ O: F: C: B# E% O' z( k<>want to create the new Recordset. </P>7 p' X8 b7 D+ N9 ]) ?0 c' b
<>source  A String specifying the source of the records for the new Recordset. </P>$ G% d2 {  r+ v4 n, d! m: I
<>The source can be a table name, a query name, or an SQL statement that </P>
2 d  R( _" ^, K1 V1 B<>returns records. For table-type Recordset objects in Microsoft Jet databases, </P>
5 l- }6 b' y* C: i6 C# C0 g6 ]<>the source can only be a table name. </P>8 X8 i: L, ?  D9 U3 ^$ M% E
<>type    Optional. A constant that indicates the type of Recordset to open, as </P>, i& d+ c/ D* `- L
<>specified in Settings. </P>
. B6 w. q9 ~) _<>options Optional. A combination of constants that specify characteristics of </P>3 t4 D$ {" ^2 z5 l8 G. f" G
<>the new Recordset, as listed in Settings. </P>4 S. i3 V$ g4 z" v
<>lockedits       Optional. A constant that determines the locking for the Recordsset, </P>
; R3 }) G+ t/ n/ y/ ?0 i- k! J3 k<P>as specified in Settings. </P>
; s1 D9 ^% k, Q; }0 O<P>Settings </P>
1 L/ ]. Y7 S3 L2 V" s% P- d. ^* _<P>  </P>. v8 @) h$ Y& H5 O
<P>You can use one of the following constants for the type argument. </P>
' W% G8 f. _3 V# u; m( q<P>  </P>
: |3 v# q7 S+ L9 k% [7 ?# u; v9 }<P>Constant        Description </P>) T* w' A! Q. G" m
<P>  </P>9 A) S4 f  _# P5 h  C9 w' R% t
<P>  </P>( u' g% |1 N- o8 `9 a
<P>dbOpenTable     Opens a table-type Recordset object (Microsoft Jet workspaces </P>
% B- j6 w" {% Y/ a<P>only). </P>
* e/ h+ H: {) g0 j' N, E" y<P>dbOpenDynamic   Opens a dynamic-type Recordset object, which is similar to an </P>+ C/ E/ f! y$ [9 Y! V/ o9 q7 |
<P>ODBC dynamic cursor. (ODBCDirect workspaces only) </P>
" V1 U2 q! @) s( D% N/ e<P>dbOpenDynaset   Opens a dynaset-type Recordset object, which is similar to an </P>
: {7 k8 Y9 V; C" j1 Q+ h# [- I<P>ODBC keyset cursor. </P>
0 U# [, w# {$ C5 S<P>dbOpenSnapshot  Opens a snapshot-type Recordset object, which is similar to an </P>
5 r" N/ x9 Q. _" M) U4 }<P>ODBC static cursor. </P>/ a4 A+ n  w/ f1 r% h+ E. N
<P>dbOpenForwardOnly?Opens a forward-only-type Recordset object. </P>
8 {; G3 h# n7 p5 v, y7 m2 U. J7 w<P>Note   If you open a Recordset in a Microsoft Jet workspace and you don't </P>: u0 @  f1 l3 |8 |, b+ V3 v
<P>specify a type, OpenRecordset creates a table-type Recordset, if possible. If </P># N1 L& X( W& v7 V+ x! ~3 o4 W
<P>you specify a linked table or query, OpenRecordset creates a dynaset-type </P>
: U+ I& j) F+ E% S3 ?& b<P>Recordset. In an ODBCDirect workspace, the default setting is dbOpenForwardOnl </P>
+ s( N% m/ h' W  F; F' C6 u<P>y. </P>3 C( D- J' {! p
<P>  </P>$ ~( }8 T/ d& s* Y1 W  ?
<P>You can use a combination of the following constants for the options </P>
$ U! N3 x/ z  V. Q/ ]2 A( ~<P>argument. </P>
% Y; {1 D/ _; ~: j# l4 o! F+ ?<P>  </P>
# @4 W: [- ~  C+ e8 E2 a<P>Constant        Description </P>& o6 H5 h+ ^( }6 Q' Q% X
<P>dbAppendOnly?Allows users to append new records to the Recordset, but </P>
0 K2 U; T6 v3 a<P>prevents them from editing or deleting existing records (Microsoft Jet </P>
; H4 F, `; }$ C# ]<P>dynaset-type Recordset only). </P>- a& j5 N" \0 o; k/ B: R
<P>dbSQLPassThrough?Passes an SQL statement to a Microsoft Jet-connected ODBC </P>
8 ~- B1 [% i$ q: J4 J6 _6 H  F<P>data source for processing (Microsoft Jet snapshot-type Recordset only). </P>( e7 r. l# \" I5 g
<P>dbSeeChanges    Generates a run-time error if one user is changing data that </P>
" q. N) H2 n$ e1 A<P>another user is editing (Microsoft Jet dynaset-type Recordset only). This is </P>
' H' A) {  R; J% I<P>useful in applications where multiple users have simultaneous read/write </P>$ F; U1 m. [* z; s1 y: T  B- g
<P>access to the same data. </P>
7 U. M3 o+ B9 C6 Z8 x" i<P>dbDenyWrite?Prevents other users from modifying or adding records (Microsoft </P>- o) W( o8 ]- k+ v/ R
<P>Jet Recordset objects only). </P>1 p' b- z- V" L, v! m  e6 M
<P>dbDenyRead?Prevents other users from reading data in a table (Microsoft Jet </P>
# T. U* g2 s: ~4 ?  m; X<P>table-type Recordset only). </P>
( a# h# F# S1 d8 U4 u<P>dbForwardOnly?Creates a forward-only Recordset (Microsoft Jet snapshot-type </P>/ E! h$ [  R) X+ J* x" e
<P>Recordset only). It is provided only for backward compatibility, and you </P>( U! Y0 b9 h. g& W$ X4 u
<P>should use the dbOpenForwardOnly constant in the type argument instead of </P>" p! V0 W* L! Q$ o! |) f
<P>using this option. </P>
& G/ N2 |/ ?6 I8 {9 U4 b0 j<P>dbReadOnly?Prevents users from making changes to the Recordset (Microsoft Jet </P>
- L+ \. J# x" j: v8 v$ Y7 d<P>only). The dbReadOnly constant in the lockedits argument replaces this </P>
. k2 X* n3 ?6 y4 Q/ w<P>option, which is provided only for backward compatibility. </P>
, o0 z' D  ^$ i# B/ }: v<P>dbRunAsync      Runs an asynchronous query (ODBCDirect workspaces only). </P>
( x1 W- G+ Y; T; `/ ?# _<P>dbExecDirect?Runs a query by skipping SQLPrepare and directly calling </P>! s# c2 n+ y! q; P& C9 B
<P>SQLExecDirect (ODBCDirect workspaces only). Use this option only when you抮e </P>
' [6 c" m6 \' _<P>not opening a Recordset based on a parameter query. For more information, see </P>
1 O' C# v9 z/ {<P>the "Microsoft ODBC 3.0 Programmer抯 Reference." </P>
4 E4 ^* R; K. B- P<P>dbInconsistent?Allows inconsistent updates (Microsoft Jet dynaset-type and </P>
9 ?4 I/ n6 l6 i7 f* j" I<P>snapshot-type Recordset objects only). </P>( G. P% w( z! C
<P>dbConsistent?Allows only consistent updates (Microsoft Jet dynaset-type and </P>
# Q1 {! `) U0 |- J9 {<P>snapshot-type Recordset objects only). </P>
4 \8 U0 M& o3 X# Q# _<P>Note   The constants dbConsistent and dbInconsistent are mutually exclusive, </P>
- }9 X1 f  f# T' i<P>and using both causes an error. Supplying a lockedits argument when options </P>
9 v6 l* u  _. f7 n5 F# J: A: C5 B<P>uses the dbReadOnly constant also causes an error. </P>% [  h: p4 S- C9 R# M/ K2 T
<P>  </P>8 R. N6 Y, o& r8 R
<P>You can use the following constants for the lockedits argument. </P>
$ Q5 M) ]; A; L+ F<P>  </P># o! }* w: v, `# M3 c8 I; ^7 Z
<P>Constant        Description </P>
5 B6 Z9 N4 W) Y) Y+ L  I# b<P>dbReadOnly      Prevents users from making changes to the Recordset (default for </P>
/ \) B! @  L- C2 e4 ?. e<P>ODBCDirect workspaces). You can use dbReadOnly in either the options argument </P>2 {" e3 u0 z7 |: ^2 |' u
<P>or the lockedits argument, but not both. If you use it for both arguments, a </P>
' A: o6 d5 `% J0 s* E+ x7 N<P>run-time error occurs. </P>
8 T4 j0 E% O$ g8 P1 ^2 D$ a4 M- ?/ n<P>dbPessimistic?Uses pessimistic locking to determine how changes are made to </P>
" X0 T, {& [" |3 D& J: d7 v<P>the Recordset in a multiuser environment. The page containing the record </P>
% s" H& s: b( u$ ?. P" ~0 m6 z<P>you're editing is locked as soon as you use the Edit method (default for </P>
0 Z# d; B+ ^+ _. x<P>Microsoft Jet workspaces). </P>  ^' Z$ G) `1 X
<P>dbOptimistic?Uses optimistic locking to determine how changes are made to the </P>
$ B% l8 _- v" Q  a, R- v. N- j3 C<P>Recordset in a multiuser environment. The page containing the record is not </P>. c# o. k3 D+ I( O! f
<P>locked until the Update method is executed. </P>
1 @* }4 _) K. N3 h* `<P>dbOptimisticValue?Uses optimistic concurrency based on row values (ODBCDirect </P>+ V9 `9 L# [' x( h: i1 e; g0 O( e
<P>workspaces only). </P>
  z9 Q) B+ Z+ j* p  Q<P>dbOptimisticBatch?Enables batch optimistic updating (ODBCDirect workspaces </P>
. q1 @: Q0 E  R$ m/ m<P>only). </P>
" m  l( |# D/ f, i2 `<P>Remarks </P>
# j1 p. n7 ]* M4 R  }' r<P>  </P>
3 G+ c- w: c# G" |9 F/ k: R<P>In a Microsoft Jet workspace, if object refers to a QueryDef object, or a </P>, O' T! e7 V$ o* m
<P>dynaset- or snapshot-type Recordset, or if source refers to an SQL statement </P>
. y! d6 B9 i8 J<P>or a TableDef that represents a linked table, you can't use dbOpenTable for </P>
/ ~# j  [+ O  C& X' G; j<P>the type argument; if you do, a run-time error occurs. If you want to use an </P>
  e  I7 G- w( R3 E<P>SQL pass-through query on a linked table in a Microsoft Jet-connected ODBC </P>! h5 {- L$ r* m( Y& i% G/ x
<P>data source, you must first set the Connect property of the linked table's </P>
- o1 a3 h" S2 e8 p7 s7 s<P>database to a valid ODBC connection string. If you only need to make a single </P>4 x0 ^' F8 N( k
<P>pass through a Recordset opened from a Microsoft Jet-connected ODBC data </P>3 ~7 M7 |- J; D2 y
<P>source, you can improve performance by using dbOpenForwardOnly for the type </P>6 d8 t$ M1 s" R: O( B; c7 W" N9 V. e
<P>argument. </P>( E5 ~7 z0 g8 |8 a( @6 q! H  G
<P>  </P>
& V$ @4 n/ m' X6 [+ ^# I: q<P>If object refers to a dynaset- or snapshot-type Recordset, the new Recordset </P>
( V& u3 G5 V6 T5 S) F<P>is of the same type object. If object </P>1 @4 E* r' I: B; d; b% T9 @( ^
<P> refers to a table-type Recordset object, the type of the new object is a </P>7 m) U- ^8 `/ A7 V2 T: Z" m/ X. N
<P>dynaset-type Recordset. You can't open new Recordset objects from forward-only </P>
3 u& c2 S$ n. e4 q+ w<P>杢ype or ODBCDirect Recordset objects. </P>
0 ^8 i$ v3 U9 B( ~<P>In an ODBCDirect workspace, you can open a Recordset containing more than one </P>
: A3 W7 A  F, V! D0 u<P>select query in the source argument, such as </P>
8 k" c4 a# I$ R<P>  </P>$ d& m* F( b% }% B) Z8 M
<P>"SELECT LastName, FirstName FROM Authors </P>: k+ }' ~6 R: x/ }; U4 V" h( |
<P>WHERE LastName = 'Smith'; </P>
+ U6 `! D$ n$ b5 D1 W" g<P>SELECT Title, ISBN FROM Titles </P>
  p) h+ P9 i5 P) d<P>WHERE ISBN Like '1-55615-*'" </P>
6 i! h' Q& y* y! _- z6 K<P>  </P>
# |, b9 g4 e7 _7 [6 q<P>The returned Recordset will open with the results of the first query. To </P>
: |3 k2 [# X! F  a/ S<P>obtain the result sets of records from subsequent queries, use the </P>
* X3 u# R- B/ s<P>NextRecordset method. </P>/ _$ t* v& Q) S
<P>  </P>
% d6 W. [- s5 L<P>Note   You can send DAO queries to a variety of different database servers </P>1 m  c- r+ ?; ~& n6 y% a
<P>with ODBCDirect, and different servers will recognize slightly different </P>
2 A* W- w7 Z: M$ b, d' M<P>dialects of SQL. Therefore, context-sensitive Help is no longer provided for </P>$ b# T8 n7 f) \' v+ B6 I6 v$ y: T/ L
<P>Microsoft Jet SQL, although online Help for Microsoft Jet SQL is still </P>, K- |& M& M$ h* S) X. H: D
<P>included through the Help menu. Be sure to check the appropriate reference </P>
1 [" a( M" D1 B2 K, C<P>documentation for the SQL dialect of your database server when using either </P>
: Q9 f) Z, ^) F* f2 j' E* I<P>ODBCDirect connections or pass-through queries in Microsoft Jet-connected </P>
; B- P2 ]4 {# @( Z0 \<P>client/server applications. </P>
+ J8 h5 m% J+ f<P>  </P>
8 Y/ z' r: V! R+ Y+ X& x) k2 C<P>Use the dbSeeChanges constant in a Microsoft Jet workspace if you want to </P>' @7 x5 i# D: B7 J5 C
<P>trap changes while two or more users are editing or deleting the same record. </P>; X9 u8 K) w3 l3 p
<P>For example, if two users start editing the same record, the first user to </P>
2 N7 d. p# d2 T* t<P>execute the Update method succeeds. When the second user invokes the Update </P>
7 q9 g* u( r+ c' O' e<P>method, a run-time error occurs. Similarly, if the second user tries to use </P>2 d" B7 e) l! D- e
<P>the Delete method to delete the record, and the first user has already </P>
; y0 Q* r7 z4 `$ W: D7 H9 @<P>changed it, a run-time error occurs. </P>- K- P9 I8 k# D' V0 j8 m: N) d% y
<P>  </P>
+ G* V% L9 x0 u; D" }1 G<P>Typically, if the user gets this error while updating a record, your code </P>
6 m# ]" I3 N' K* }0 v& y- q$ E, w7 S<P>should refresh the contents of the fields and retrieve the newly modified </P>
0 M2 T4 ~6 F' K1 ?<P>values. If the error occurs while deleting a record, your code could display </P>
3 R% L& ^3 X' F- D; x) K( @, [7 Z7 b1 b4 C<P>the new record data to the user and a message indicating that the data has </P>
0 y" M2 U& }1 o, b; X3 G<P>recently changed. At this point, your code can request a confirmation that </P>% Y1 ]3 j, h9 w8 j  _
<P>the user still wants to delete the record. </P>2 H1 Z) I$ k0 H9 h7 N1 c
<P>  </P>
+ c( x9 l1 z5 u' V0 ^! }$ h. A! O<P>You should also use the dbSeeChanges constant if you open a Recordset in a </P>: e0 }; U: A9 I. A+ k' t
<P>Microsoft Jet-connected ODBC workspace against a Microsoft SQL Server 6.0 (or </P>
/ g. P+ Q/ D" G! T6 }<P>later) table that has an IDENTITY column, otherwise an error may result. </P>
2 c" {1 q8 O9 n$ O4 c! B. x" I8 Z<P>  </P>
/ e3 s2 y  i3 h5 W5 Y$ Z! C6 c0 i<P>In an ODBCDirect workspace, you can execute asynchronous queries by setting </P>  t7 }  w5 r) E( v
<P>the dbRunAsync constant in the options argument. This allows your application </P>+ I  o. K# u1 k/ i3 c, _4 n& l, ]! T
<P>to continue processing other statements while the query runs in the </P>" Y5 W/ K- ?# o2 A1 V
<P>background. But, you cannot access the Recordset data until the query has </P>/ s7 W0 V# r$ X1 t+ f; o4 r
<P>completed. To determine whether the query has finished executing, check the </P>/ p% w# \3 x2 L- n2 s- a- d
<P>StillExecuting property of the new Recordset. If the query takes longer to </P>
" }8 d0 r8 h- W: z! A/ r# J<P>complete than you anticipated, you can terminate execution of the query with </P>
. k3 u  M0 e+ J. L: h2 O<P>the Cancel method. </P>2 q* S( s" O8 {. l8 w9 n+ ?
<P>  </P>/ L  a# K+ G5 W3 M, p; ~& ~8 ~' i9 ]
<P>Opening more than one Recordset on an ODBC data source may fail because the </P>0 G" l8 e. g( a3 K' V7 P
<P>connection is busy with a prior </P>; u% f# W1 N3 U
<P>OpenRecordset call. One way around this is to use a server-side cursor and </P>3 `8 `, _$ }1 h. j  K
<P>ODBCDirect, if the server supports this. Another solution is to fully </P>( u1 A- [' d5 k. s( ?
<P>populate the Recordset by using the MoveLast method as soon as the Recordset </P>6 o% L, L' F2 g& D
<P>is opened. </P>
) X" e2 t7 h1 W( K<P>  </P>4 }0 |" @& A. [; B- J
<P>If you open a Connection object with DefaultCursorDriver set to </P>$ Z% ]% X8 H- N6 i" z! r
<P>dbUseClientBatchCursor, you can open a Recordset to cache changes to the data </P>
  {0 }' k% z8 L( y<P>(known as batch updating) in an ODBCDirect workspace. Include dbOptimisticBatc </P>
3 ?  Q% j+ V. u. c/ p3 S<P>h in the lockedits argument to enable update caching. See the Update method </P>
; l6 v. G9 b4 }: l1 {7 F$ X5 T/ V( J<P>topic for details about how to write changes to disk immediately, or to cache </P>& h3 J3 T0 k6 n3 p
<P>changes and write them to disk as a batch. </P>0 K* c7 [! U9 [) w$ I
<P>  </P>9 r! p  Z5 g4 u( h( @2 e1 v
<P>Closing a Recordset with the Close method automatically deletes it from the </P>2 Q. ^, w# I# ^6 w) O# H
# R' w3 _% s( @' Q& H
<P>Recordsets collection. </P>" v4 \) _- }3 H: ~2 _& P
<P>  </P>0 Y- z1 D8 b% S/ K' H: L, u
<P>Note   If source refers to an SQL statement composed of a string concatenated </P>6 S+ _- [' m" Q- ~
<P>with a non-integer value, and the system parameters specify a non-U.S. </P>! m* \9 O* x0 ?6 r
<P>decimal character such as a comma (for example, strSQL = "PRICE &gt; " &amp; </P>4 L; V9 L1 I5 `" s$ ^) D
<P>lngPrice, and lngPrice = 125,50), an error occurs when you try to open the </P>
8 v) d6 b- w- A) U7 D. u( H<P>Recordset. This is because during concatenation, the number will be converted </P>
1 S. ]% c( H2 {* J  T<P>to a string using your system's default decimal character, and SQL only </P>
* g# t% Y2 y: n. \<P>accepts U.S. decimal characters.</P>




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