QQ登录

只需要一步,快速开始

 注册地址  找回密码
查看: 2585|回复: 0
打印 上一主题 下一主题

怎样用ADO打开一个带密码的Access库?

[复制链接]
字体大小: 正常 放大
韩冰        

823

主题

3

听众

4048

积分

我的地盘我做主

该用户从未签到

发帖功臣 元老勋章

跳转到指定楼层
1#
发表于 2005-1-26 12:38 |只看该作者 |倒序浏览
|招呼Ta 关注Ta
<>  </P>1 t' c, ^/ i1 O2 q6 K
<>Creates a new Recordset object and appends it to the Recordsets collection. </P># Z4 K% f( r; ?  L! g
<>  </P>% W9 h3 {6 Q5 e0 ]% _" |* H- ]
<>Syntax </P>
1 Y3 E. C  v; ~5 z' N; b+ A4 t<>  </P>. }1 L) J1 P& p2 D8 {7 ^
<>For Connection and Database objects: </P>( u& S- B( u7 Q3 l# w! o$ a9 h
<>  </P>
% m1 V# W1 ~$ z! t0 O8 x* s<>Set recordset = object.OpenRecordset (source, type, options, lockedits) </P>) t( V2 {' |6 p- y) O
<>  </P>
; G' A! A6 f0 }4 N: P) }, H<>For QueryDef, Recordset, and TableDef objects: </P>
# Z1 M1 O7 r- @% d3 Z0 w- g) a, x) Y<>  </P>
7 N/ R0 ]. W+ e4 S* l1 R( A$ l. L0 z<>Set recordset = object.OpenRecordset (type, options, lockedits) </P>; k* s- i* F, k: K) O% x, k$ P
<>  </P>
6 p( V/ ~8 i. }$ {<>The OpenRecordset method syntax has these parts. </P>
- A: c# i0 K6 T: x<>  </P>0 ?  t: s3 t1 S/ ?% n6 H' f) J" V  |2 E
<>art    Description </P>8 {" |/ D. \- p) {! U- R" w
<>recordset       An object variable that represents the Recordset object you wantt to </P>
+ C! K6 w( I, L  v# z+ |1 u5 s& A<>open. </P>: D0 Z: U% M& P1 [( ]- e
<>object  An object variable that represents an existing object from which you </P>
( S7 |1 Z% r2 ?. z0 F- I<>want to create the new Recordset. </P># [& L+ ^7 w2 r. W" P$ b3 b& B
<>source  A String specifying the source of the records for the new Recordset. </P>( l" ?/ Y+ J$ ^/ k
<>The source can be a table name, a query name, or an SQL statement that </P>
3 w" ^8 G6 A" y3 q<>returns records. For table-type Recordset objects in Microsoft Jet databases, </P>+ `  \# C, l, }, y6 {) w: Q# e1 t5 t
<>the source can only be a table name. </P>! K  B3 v, i8 Y& f/ B4 j* u
<>type    Optional. A constant that indicates the type of Recordset to open, as </P>
6 B) [) i- j# ]$ B: W# u" K: I<>specified in Settings. </P>
( n& L1 R0 i5 ^8 E% D7 d  u<>options Optional. A combination of constants that specify characteristics of </P>
0 l! r! s8 k  z' l3 |<>the new Recordset, as listed in Settings. </P>
5 U3 X. V$ J9 P' M# h; {8 H6 \<>lockedits       Optional. A constant that determines the locking for the Recordsset, </P>; Y% D/ N3 |4 S
<P>as specified in Settings. </P>
0 M  @; o8 f. y& d" x& I<P>Settings </P>
4 _  @  E/ Y$ c' C: i; b+ m" I<P>  </P>
0 w: m7 o# X, t* X8 [" `<P>You can use one of the following constants for the type argument. </P>: l! {- U4 {: V, H
<P>  </P>
& N7 ?. Q# N( A. o/ r<P>Constant        Description </P>
7 `# @8 b. @4 f$ {<P>  </P>
# f; D  I! f2 W! |$ l<P>  </P>  K% u# [* ^# W" y4 }6 @
<P>dbOpenTable     Opens a table-type Recordset object (Microsoft Jet workspaces </P>
) \, N$ o! X0 b+ I<P>only). </P>: @7 M- ~* p) r3 [- I
<P>dbOpenDynamic   Opens a dynamic-type Recordset object, which is similar to an </P>
. I$ x5 G0 J/ H4 `, E, Y<P>ODBC dynamic cursor. (ODBCDirect workspaces only) </P>
3 `* j1 y$ J& S3 E' U<P>dbOpenDynaset   Opens a dynaset-type Recordset object, which is similar to an </P>9 c& [: E% C+ ^3 l& D5 e2 V1 {7 L
<P>ODBC keyset cursor. </P>
# W1 O3 V% r" {9 Z  }<P>dbOpenSnapshot  Opens a snapshot-type Recordset object, which is similar to an </P>6 Q' s9 W, i# m* B) C, K
<P>ODBC static cursor. </P>
$ C' H- i1 d  i6 a<P>dbOpenForwardOnly?Opens a forward-only-type Recordset object. </P>
- \% `) a6 Q+ R: N# I<P>Note   If you open a Recordset in a Microsoft Jet workspace and you don't </P>
- k, C$ c2 ?! x<P>specify a type, OpenRecordset creates a table-type Recordset, if possible. If </P>) @4 `# r( J% `5 @
<P>you specify a linked table or query, OpenRecordset creates a dynaset-type </P>9 `8 s2 v/ i6 o1 P, S7 Z0 p1 A4 l
<P>Recordset. In an ODBCDirect workspace, the default setting is dbOpenForwardOnl </P>
8 `( G. m" L6 X' m* ?* @<P>y. </P>7 k% K* i& Q% M1 I/ @1 u2 e+ \
<P>  </P>. N& I8 \  q8 ^( F0 A! u* o
<P>You can use a combination of the following constants for the options </P>
. \! J4 M7 a1 I" ^9 x9 r3 U<P>argument. </P>+ U' [1 f+ N( ^7 y4 ^
<P>  </P>
+ E' @+ N7 v1 r$ V9 @0 D! Z% J<P>Constant        Description </P>0 m8 V1 P( a' ~. g5 z
<P>dbAppendOnly?Allows users to append new records to the Recordset, but </P>
4 B& L6 B- k+ x% j: F* L9 ?<P>prevents them from editing or deleting existing records (Microsoft Jet </P>7 p: l/ C2 w" q2 Q' ?& x: I* {
<P>dynaset-type Recordset only). </P>/ }- n  h# h. c) Q* C1 C
<P>dbSQLPassThrough?Passes an SQL statement to a Microsoft Jet-connected ODBC </P>
! b, o, n+ m6 B0 U$ E1 p<P>data source for processing (Microsoft Jet snapshot-type Recordset only). </P>, d7 O* \, G% N% N* I  k
<P>dbSeeChanges    Generates a run-time error if one user is changing data that </P>/ H$ t' r5 d; `0 n2 N6 H& L  [. U
<P>another user is editing (Microsoft Jet dynaset-type Recordset only). This is </P>
' M2 x- }8 C0 s5 b" s0 C: G<P>useful in applications where multiple users have simultaneous read/write </P>" z* o6 e# c) l% B& o/ L
<P>access to the same data. </P>
1 l8 g1 D1 D5 a- \<P>dbDenyWrite?Prevents other users from modifying or adding records (Microsoft </P>& J& A0 k$ G: _" j
<P>Jet Recordset objects only). </P>- ]2 \( R+ I- h
<P>dbDenyRead?Prevents other users from reading data in a table (Microsoft Jet </P>
* |/ M# Y! y7 n<P>table-type Recordset only). </P>! U1 t: V3 b" h& J: Y7 o
<P>dbForwardOnly?Creates a forward-only Recordset (Microsoft Jet snapshot-type </P>
! M3 C( ]( x+ T3 L8 o<P>Recordset only). It is provided only for backward compatibility, and you </P>
( v) {" j  l' T- L. k<P>should use the dbOpenForwardOnly constant in the type argument instead of </P>
+ r% u  T& P9 \# T  N! v- V0 k<P>using this option. </P>
% ]# ?* d5 x4 E; X) S<P>dbReadOnly?Prevents users from making changes to the Recordset (Microsoft Jet </P>
$ z+ M$ L( S& ?, L<P>only). The dbReadOnly constant in the lockedits argument replaces this </P>0 o3 u% M* k7 D* n3 X# A
<P>option, which is provided only for backward compatibility. </P>/ g" Q6 r$ c+ [; j! Z7 c) u; {8 e
<P>dbRunAsync      Runs an asynchronous query (ODBCDirect workspaces only). </P>
  H& [$ x( w* f' V( l) I- ~<P>dbExecDirect?Runs a query by skipping SQLPrepare and directly calling </P>
% Y" Y: e" x) r: b3 P& n3 ?<P>SQLExecDirect (ODBCDirect workspaces only). Use this option only when you抮e </P>. s; D- U* @. P4 h; Z' q
<P>not opening a Recordset based on a parameter query. For more information, see </P>
" s; f( x2 u8 A: E+ Q<P>the "Microsoft ODBC 3.0 Programmer抯 Reference." </P>
: G% o' t: e) X<P>dbInconsistent?Allows inconsistent updates (Microsoft Jet dynaset-type and </P>/ X2 L  X) _. x, G
<P>snapshot-type Recordset objects only). </P>, T5 l9 D0 \* P" A. L& J* C& |
<P>dbConsistent?Allows only consistent updates (Microsoft Jet dynaset-type and </P>8 p4 V3 R- _) C: M4 @% \8 R
<P>snapshot-type Recordset objects only). </P>3 }- V% ~" r7 A9 Q
<P>Note   The constants dbConsistent and dbInconsistent are mutually exclusive, </P># r2 U# F. N# [. v
<P>and using both causes an error. Supplying a lockedits argument when options </P>5 x. W! _; s# L% R
<P>uses the dbReadOnly constant also causes an error. </P>
8 e# V- ^2 }2 K<P>  </P>
2 ~$ `% H$ C4 C; F, y<P>You can use the following constants for the lockedits argument. </P>7 Z! h4 T# C7 n
<P>  </P>  p% i+ f/ W* U* g
<P>Constant        Description </P>
; x( o6 J- o! C( W9 V8 ?7 i<P>dbReadOnly      Prevents users from making changes to the Recordset (default for </P>4 J! i& f# u9 A. N
<P>ODBCDirect workspaces). You can use dbReadOnly in either the options argument </P>
' f. ?8 Y2 I: }5 m# i/ J8 k9 n<P>or the lockedits argument, but not both. If you use it for both arguments, a </P>
- s# X& g  Q3 L1 i<P>run-time error occurs. </P>
' L( `; W9 [2 J/ q; n1 S+ w<P>dbPessimistic?Uses pessimistic locking to determine how changes are made to </P>  T0 V# g8 X( O2 U' j5 F! Z
<P>the Recordset in a multiuser environment. The page containing the record </P>; v: G) g$ q9 Y& l9 E
<P>you're editing is locked as soon as you use the Edit method (default for </P>. q4 q4 Q+ c* P* m( G' Z
<P>Microsoft Jet workspaces). </P>
0 ~8 S6 W3 T: N9 y" T+ U<P>dbOptimistic?Uses optimistic locking to determine how changes are made to the </P>
- S! x# ]- r4 Y! Z<P>Recordset in a multiuser environment. The page containing the record is not </P>- k, j4 {8 J) k6 O
<P>locked until the Update method is executed. </P>
% C+ c  M$ o. Y2 M9 _* j$ Z<P>dbOptimisticValue?Uses optimistic concurrency based on row values (ODBCDirect </P>
5 ?+ x, h9 Y7 A5 _<P>workspaces only). </P>2 j! z- Y, M: v- D) l9 m* E) w
<P>dbOptimisticBatch?Enables batch optimistic updating (ODBCDirect workspaces </P>! |8 p* H9 ~6 t$ K) p/ t- Y
<P>only). </P>
* B2 l2 m5 K. t<P>Remarks </P>) V: L+ l& O: Y9 P7 P) o
<P>  </P>  X& x) E+ q3 r- C
<P>In a Microsoft Jet workspace, if object refers to a QueryDef object, or a </P>
; ~* \4 Q8 H3 ?3 Z3 z& ~* H<P>dynaset- or snapshot-type Recordset, or if source refers to an SQL statement </P>$ z5 M! D+ ~7 I
<P>or a TableDef that represents a linked table, you can't use dbOpenTable for </P>
2 P0 N/ u/ K, P; y<P>the type argument; if you do, a run-time error occurs. If you want to use an </P>
/ ^- |* W! K& C% e, R; X* X<P>SQL pass-through query on a linked table in a Microsoft Jet-connected ODBC </P>
9 w0 o) s7 O- J% y3 \  b9 T/ L<P>data source, you must first set the Connect property of the linked table's </P>! Z/ H1 }' p, O6 \
<P>database to a valid ODBC connection string. If you only need to make a single </P>: T  O8 |/ Y# U( Y
<P>pass through a Recordset opened from a Microsoft Jet-connected ODBC data </P># N- j; D  a+ A
<P>source, you can improve performance by using dbOpenForwardOnly for the type </P>
! y! R$ [. s4 S% F  j<P>argument. </P>1 B$ a; _. \7 S1 H0 s5 e9 l
<P>  </P>1 u/ `8 F( b4 ~) [) S
<P>If object refers to a dynaset- or snapshot-type Recordset, the new Recordset </P>
0 ^" Q1 N# W8 ?9 Y3 j<P>is of the same type object. If object </P>$ T; F9 K' o( E* i: j4 X; e4 V. C
<P> refers to a table-type Recordset object, the type of the new object is a </P>% ~& S6 y9 C" l5 a( m
<P>dynaset-type Recordset. You can't open new Recordset objects from forward-only </P>
5 N2 @) w, u8 S0 T  J<P>杢ype or ODBCDirect Recordset objects. </P>
) g- H$ q& Z  c  g<P>In an ODBCDirect workspace, you can open a Recordset containing more than one </P>
6 T8 W; u1 L( ]# I' y. j<P>select query in the source argument, such as </P>
, \% l! |; T/ V# f) s; c* n* g<P>  </P>
% Q* }; j( y* }5 s<P>"SELECT LastName, FirstName FROM Authors </P>: }+ X. q$ q, R2 }: Q0 E& B* G7 U
<P>WHERE LastName = 'Smith'; </P>
8 o8 X3 R) \/ r  v$ ~$ J: a<P>SELECT Title, ISBN FROM Titles </P>
& W! w7 i/ R2 U' D- g( M<P>WHERE ISBN Like '1-55615-*'" </P>
) b( m  x% ^3 F  F5 F! Z<P>  </P>  A3 ~+ H+ a5 R# ^9 J3 ^
<P>The returned Recordset will open with the results of the first query. To </P>
% l* o' b  f  e0 C/ j<P>obtain the result sets of records from subsequent queries, use the </P>
, ^0 e2 ?- y) J<P>NextRecordset method. </P>1 a$ @5 N9 A+ d5 g
<P>  </P>
+ f0 b* P1 Y5 L4 S+ }, M" e- U<P>Note   You can send DAO queries to a variety of different database servers </P>; q3 \2 b/ K5 ^* t3 S
<P>with ODBCDirect, and different servers will recognize slightly different </P>! G7 S: n( G' v7 }+ Y
<P>dialects of SQL. Therefore, context-sensitive Help is no longer provided for </P>
2 i: H: \4 g1 e5 p7 p) m) S<P>Microsoft Jet SQL, although online Help for Microsoft Jet SQL is still </P>0 D9 C5 D6 E/ ~/ k$ S
<P>included through the Help menu. Be sure to check the appropriate reference </P>
( h2 }5 u0 s: Y! J* Z+ E  U<P>documentation for the SQL dialect of your database server when using either </P>
* y' R/ f, _4 w% T<P>ODBCDirect connections or pass-through queries in Microsoft Jet-connected </P>9 z! Z% U. h# x' }5 `
<P>client/server applications. </P>" V7 V' l, q" E# e
<P>  </P>
1 P3 _# J% ^; D  z8 d  W0 e<P>Use the dbSeeChanges constant in a Microsoft Jet workspace if you want to </P>
7 r  ]& s! w& m) i( V7 Y( w) V<P>trap changes while two or more users are editing or deleting the same record. </P>7 @1 v% e  F9 M- z! j4 x- y; j
<P>For example, if two users start editing the same record, the first user to </P>
2 k! q1 t- g6 Y5 G' m% s; S+ W& J<P>execute the Update method succeeds. When the second user invokes the Update </P>
( Q. g3 r& l& d- v8 A<P>method, a run-time error occurs. Similarly, if the second user tries to use </P>& F1 H( U% I$ D$ x! \
<P>the Delete method to delete the record, and the first user has already </P>" X! _' B" E- c
<P>changed it, a run-time error occurs. </P>
: n4 D* y( U9 J7 E0 g% f: ?2 s, T0 f0 l<P>  </P>. R( J: k' |5 [7 n
<P>Typically, if the user gets this error while updating a record, your code </P>5 R2 ?. D# b4 j
<P>should refresh the contents of the fields and retrieve the newly modified </P>/ Z( u4 z. N' \, @9 @* \0 k; g
<P>values. If the error occurs while deleting a record, your code could display </P>
) d( Q+ p& ~. q- I<P>the new record data to the user and a message indicating that the data has </P>  U# \* |7 j/ N9 |5 J: I5 e
<P>recently changed. At this point, your code can request a confirmation that </P>  o- A! i% d+ k& P% `
<P>the user still wants to delete the record. </P>2 @( V! B. `% O
<P>  </P>
* V4 G1 a- G3 p. h/ g0 w<P>You should also use the dbSeeChanges constant if you open a Recordset in a </P>
" ~3 |& c) _1 M4 D9 P& {( X1 j<P>Microsoft Jet-connected ODBC workspace against a Microsoft SQL Server 6.0 (or </P>
% t. c! I8 {! x9 U( l0 A; L<P>later) table that has an IDENTITY column, otherwise an error may result. </P>
" I2 h: j7 E! W. m* r3 z, t9 t$ a<P>  </P>5 N3 r' A% @% I' c2 G6 m) ^6 Y3 J4 \
<P>In an ODBCDirect workspace, you can execute asynchronous queries by setting </P>
3 n+ r' W2 }& [4 }: l<P>the dbRunAsync constant in the options argument. This allows your application </P>
, i9 z' x. o, [/ V  Q<P>to continue processing other statements while the query runs in the </P>" @/ U% q& q+ i
<P>background. But, you cannot access the Recordset data until the query has </P>5 ~3 a4 K, u' |7 |6 v1 T8 P
<P>completed. To determine whether the query has finished executing, check the </P>" V  r# F) u. M! ~+ M
<P>StillExecuting property of the new Recordset. If the query takes longer to </P># X' [+ |' K) H
<P>complete than you anticipated, you can terminate execution of the query with </P>
3 d9 `1 o. Z+ [5 R! \4 l<P>the Cancel method. </P>
" D) H& O6 [# h' X: ^2 y2 S<P>  </P>3 d2 i! {" \) ~8 O% d1 t$ ]
<P>Opening more than one Recordset on an ODBC data source may fail because the </P>
" e  E* ]: n; O+ z  e& i7 W' q) X; x4 L- Y<P>connection is busy with a prior </P>- y7 \1 ?6 L1 D4 t' t% T
<P>OpenRecordset call. One way around this is to use a server-side cursor and </P>
6 J" D/ P" p' Q' d5 W" u; k<P>ODBCDirect, if the server supports this. Another solution is to fully </P>' N6 E9 J: e7 m8 W# I: o  S' Q
<P>populate the Recordset by using the MoveLast method as soon as the Recordset </P>
5 \% S5 [  C2 r, U3 d<P>is opened. </P>! P( H6 A' F7 B) D. Z0 @- T
<P>  </P>
# L) H( S4 g. z0 o6 m' T<P>If you open a Connection object with DefaultCursorDriver set to </P>
$ Q' G- o2 x# b$ z! [9 i; Q  o<P>dbUseClientBatchCursor, you can open a Recordset to cache changes to the data </P>) F+ y% o/ e+ B8 @) h( z
<P>(known as batch updating) in an ODBCDirect workspace. Include dbOptimisticBatc </P>
6 Q2 d, Z0 Q2 r/ E& [+ R* x3 J1 d<P>h in the lockedits argument to enable update caching. See the Update method </P>( {' d  Z4 l5 k% e& K
<P>topic for details about how to write changes to disk immediately, or to cache </P>
# B7 g+ ?% \6 L4 j+ Z. V& y6 ]<P>changes and write them to disk as a batch. </P>' `  t- D9 |  ~( x4 E$ D" Y
<P>  </P>
. k$ T3 F4 N; _2 T2 v8 |. J<P>Closing a Recordset with the Close method automatically deletes it from the </P>& y. W+ D# q4 @- L  j( ?& ]. w# q

  h# c" J' q( G- R% g<P>Recordsets collection. </P>" j2 D5 S; z/ ?* ^
<P>  </P>
0 N2 S' c" i' Z0 }, F<P>Note   If source refers to an SQL statement composed of a string concatenated </P>
) A$ J* Y3 C; \  b<P>with a non-integer value, and the system parameters specify a non-U.S. </P>' O& t/ S1 i, c: }  N
<P>decimal character such as a comma (for example, strSQL = "PRICE &gt; " &amp; </P>
. b- J" }, Q% A/ b<P>lngPrice, and lngPrice = 125,50), an error occurs when you try to open the </P>
' |+ W- n5 h! |<P>Recordset. This is because during concatenation, the number will be converted </P>
& S- S8 V/ S+ t: D<P>to a string using your system's default decimal character, and SQL only </P>
* X. D, z% Z7 c! k<P>accepts U.S. decimal characters.</P>
zan
转播转播0 分享淘帖0 分享分享0 收藏收藏0 支持支持0 反对反对0 微信微信
您需要登录后才可以回帖 登录 | 注册地址

qq
收缩
  • 电话咨询

  • 04714969085
fastpost

关于我们| 联系我们| 诚征英才| 对外合作| 产品服务| QQ

手机版|Archiver| |繁體中文 手机客户端  

蒙公网安备 15010502000194号

Powered by Discuz! X2.5   © 2001-2013 数学建模网-数学中国 ( 蒙ICP备14002410号-3 蒙BBS备-0002号 )     论坛法律顾问:王兆丰

GMT+8, 2026-4-10 11:32 , Processed in 0.311504 second(s), 52 queries .

回顶部