QQ登录

只需要一步,快速开始

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

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

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

823

主题

3

听众

4048

积分

我的地盘我做主

该用户从未签到

发帖功臣 元老勋章

跳转到指定楼层
1#
发表于 2005-1-26 12:38 |只看该作者 |倒序浏览
|招呼Ta 关注Ta
<>  </P>+ z: M' \) g$ k1 `
<>Creates a new Recordset object and appends it to the Recordsets collection. </P>
* |. X, `8 T! I. M0 J1 p<>  </P>
5 K+ K7 U% G9 t' c2 a$ M<>Syntax </P>% Y0 _0 L2 r: R' G/ G2 C
<>  </P>' O$ D  M7 d3 v' Y& G
<>For Connection and Database objects: </P>
, D7 P$ Y3 Q& T" ~% Q; r& W<>  </P>
/ c3 t/ C& u7 ~3 a7 y5 j( T<>Set recordset = object.OpenRecordset (source, type, options, lockedits) </P>) \8 E8 e: ]; ~% u# p" o4 \
<>  </P>
3 \0 z% ?1 W/ {<>For QueryDef, Recordset, and TableDef objects: </P>
% q& q: w! w8 Z$ W<>  </P>
$ Y& M, G" c. L$ R<>Set recordset = object.OpenRecordset (type, options, lockedits) </P>! n8 v$ u8 |9 b6 {- U: t
<>  </P>
, d: B. V7 t8 }" |4 p+ b: {. J3 S<>The OpenRecordset method syntax has these parts. </P>/ n9 C: x- J. Z; _
<>  </P>0 Q1 v% E1 N8 Q8 c" h
<>art    Description </P>
; `" [& V9 R; Y<>recordset       An object variable that represents the Recordset object you wantt to </P>
' |2 p% a# e; E<>open. </P># }6 Y/ b- L) q
<>object  An object variable that represents an existing object from which you </P>
- r- J* z% |2 @! T* N0 P+ N' t<>want to create the new Recordset. </P>
( S3 Z! C; i4 b! j<>source  A String specifying the source of the records for the new Recordset. </P>
, M8 R/ D! f; V<>The source can be a table name, a query name, or an SQL statement that </P>
: d' Q/ z! p; e! E1 n$ ~6 h<>returns records. For table-type Recordset objects in Microsoft Jet databases, </P>$ C1 v( X8 ^6 t! e0 g
<>the source can only be a table name. </P>' R) z3 {  x! K$ L" B: ~( t
<>type    Optional. A constant that indicates the type of Recordset to open, as </P>
# C- k/ b/ [8 d0 V, R* {; O/ w<>specified in Settings. </P>4 l0 D; p9 b1 g
<>options Optional. A combination of constants that specify characteristics of </P>- q* W& g$ K9 u' q
<>the new Recordset, as listed in Settings. </P>5 H9 ?' w" s+ |4 ~
<>lockedits       Optional. A constant that determines the locking for the Recordsset, </P>
8 q- y$ v" t% d1 o  n& M5 q" n<P>as specified in Settings. </P>
$ c: _% N) W( e<P>Settings </P>5 s$ [& I. N( F0 {, X% A; f
<P>  </P>
2 x$ ]1 Z3 a' U1 o+ G<P>You can use one of the following constants for the type argument. </P>. s3 v* C+ w: @$ j- t: ~$ w6 O. a4 `
<P>  </P>
! D- n( f) p9 p) x4 B0 N: f<P>Constant        Description </P># ?4 T7 _* s( i5 Z% V
<P>  </P>  c1 X) Y) u$ ?/ {" Y
<P>  </P>  p) x. w: B" s7 R- {$ C" B; s4 F
<P>dbOpenTable     Opens a table-type Recordset object (Microsoft Jet workspaces </P>" V! M7 {; u# V+ c3 {% s' ^; ]
<P>only). </P>
) y4 M/ t1 ?+ [1 W4 S- }# r7 k<P>dbOpenDynamic   Opens a dynamic-type Recordset object, which is similar to an </P>
8 ]- u( g6 E  i6 A8 ?<P>ODBC dynamic cursor. (ODBCDirect workspaces only) </P>
5 g6 b* j3 R5 X( |! Z* j7 ]<P>dbOpenDynaset   Opens a dynaset-type Recordset object, which is similar to an </P>( z8 l) e0 M( C! H1 F* S
<P>ODBC keyset cursor. </P>
1 J( z0 {+ @* d1 L3 S$ N7 v<P>dbOpenSnapshot  Opens a snapshot-type Recordset object, which is similar to an </P>5 D) s: z: L- J; ^# W) A0 `
<P>ODBC static cursor. </P>
+ Y$ [& B$ u- b<P>dbOpenForwardOnly?Opens a forward-only-type Recordset object. </P>
! _6 S3 I& N- L$ D6 U<P>Note   If you open a Recordset in a Microsoft Jet workspace and you don't </P>- t6 ?& f) R7 \5 o3 f. i9 h/ v1 L
<P>specify a type, OpenRecordset creates a table-type Recordset, if possible. If </P>& B4 f$ i% i. n& [3 t
<P>you specify a linked table or query, OpenRecordset creates a dynaset-type </P>! U6 J* }# G: l1 E4 C
<P>Recordset. In an ODBCDirect workspace, the default setting is dbOpenForwardOnl </P>
9 Q! }0 K: k+ @5 J3 ]; ]# c. w<P>y. </P>
. q) _. Q: |, F) Z0 V/ x<P>  </P>
$ [) K* ?! i, T. s$ D) |) h" K<P>You can use a combination of the following constants for the options </P>
# r: C& u7 u1 l1 c3 t7 X3 D" S2 v<P>argument. </P>
1 _2 j% i" g0 v9 F% m/ _<P>  </P>* [3 G) _0 }' {# U1 |" H# Q0 M
<P>Constant        Description </P>! X' w+ Q; W: G5 S) F) K
<P>dbAppendOnly?Allows users to append new records to the Recordset, but </P>' m  n2 h$ F% x7 U
<P>prevents them from editing or deleting existing records (Microsoft Jet </P>% z! d0 I* ^& T7 T5 ?
<P>dynaset-type Recordset only). </P>8 u+ r5 ?% e+ Q- L2 A! u
<P>dbSQLPassThrough?Passes an SQL statement to a Microsoft Jet-connected ODBC </P>
+ [0 h3 u' z" C& J; `<P>data source for processing (Microsoft Jet snapshot-type Recordset only). </P>, Y4 O& V  W" q) g! g0 D7 l
<P>dbSeeChanges    Generates a run-time error if one user is changing data that </P>4 s3 [2 a0 m7 w8 P; W" ]8 ?
<P>another user is editing (Microsoft Jet dynaset-type Recordset only). This is </P>+ m6 l0 L, y5 `1 L  s; z
<P>useful in applications where multiple users have simultaneous read/write </P>
  M, R! B1 y, e8 E<P>access to the same data. </P>/ N- S6 v1 z* N; n$ j  P
<P>dbDenyWrite?Prevents other users from modifying or adding records (Microsoft </P>/ i+ _  w4 o- X' l( j+ o
<P>Jet Recordset objects only). </P>
* U" b1 }% Q% s7 T& z7 P<P>dbDenyRead?Prevents other users from reading data in a table (Microsoft Jet </P>1 n7 p- N+ s' J
<P>table-type Recordset only). </P>& s; A- f) W( g! n
<P>dbForwardOnly?Creates a forward-only Recordset (Microsoft Jet snapshot-type </P>
* o; v' H7 Y  U# Y: x0 T<P>Recordset only). It is provided only for backward compatibility, and you </P>* g& ], j) N. g5 |0 x4 c* m
<P>should use the dbOpenForwardOnly constant in the type argument instead of </P>. z1 x, Y3 U* x, M" N
<P>using this option. </P>
/ L' q* M! B6 x' p<P>dbReadOnly?Prevents users from making changes to the Recordset (Microsoft Jet </P>( n/ r! I7 p# s9 X1 J: A; p
<P>only). The dbReadOnly constant in the lockedits argument replaces this </P>
# v4 G' e6 s3 T8 ]7 A9 R9 W# u) W<P>option, which is provided only for backward compatibility. </P>
; |6 X3 S: a+ c! V<P>dbRunAsync      Runs an asynchronous query (ODBCDirect workspaces only). </P>8 F, R: F2 l# W# T! N
<P>dbExecDirect?Runs a query by skipping SQLPrepare and directly calling </P>
! h$ X! E8 s  s' _' p6 S<P>SQLExecDirect (ODBCDirect workspaces only). Use this option only when you抮e </P>5 M! i/ b6 u2 j
<P>not opening a Recordset based on a parameter query. For more information, see </P>: b! g2 s3 g& N
<P>the "Microsoft ODBC 3.0 Programmer抯 Reference." </P>
3 Q, |2 f; o8 _6 C" K6 G  q- I<P>dbInconsistent?Allows inconsistent updates (Microsoft Jet dynaset-type and </P>$ u( @2 I6 n; V7 Z. d
<P>snapshot-type Recordset objects only). </P>
, @4 q8 P2 F$ w0 ~<P>dbConsistent?Allows only consistent updates (Microsoft Jet dynaset-type and </P>' T, c7 u! W: m4 e
<P>snapshot-type Recordset objects only). </P>
$ B8 b9 Y' N5 _; B7 E6 v0 `! m<P>Note   The constants dbConsistent and dbInconsistent are mutually exclusive, </P>: {+ E" C% |9 T# }
<P>and using both causes an error. Supplying a lockedits argument when options </P>, P  O; W; v" A3 v; L5 H3 k3 K: [
<P>uses the dbReadOnly constant also causes an error. </P>
3 {( R$ {' \+ K; O! a. |<P>  </P>
8 g7 l+ L" R9 }<P>You can use the following constants for the lockedits argument. </P># I0 {' y( l, P
<P>  </P>/ x8 k* _+ x. Q# v- t' q
<P>Constant        Description </P>
9 u: x! S7 _3 ^; f1 Z+ W  C<P>dbReadOnly      Prevents users from making changes to the Recordset (default for </P>
4 h" C) Q- o1 N' e2 d9 x# d9 n# Z<P>ODBCDirect workspaces). You can use dbReadOnly in either the options argument </P>
' [1 ?: G6 q- }* Q; d3 Z9 C2 n<P>or the lockedits argument, but not both. If you use it for both arguments, a </P>% I, G! f0 ?7 |) M5 u
<P>run-time error occurs. </P>3 R) P5 Z1 i* ^) u7 n$ t0 ^
<P>dbPessimistic?Uses pessimistic locking to determine how changes are made to </P>
/ P3 y0 k% I2 z7 L<P>the Recordset in a multiuser environment. The page containing the record </P>8 o& [5 M, j2 ~; }$ `
<P>you're editing is locked as soon as you use the Edit method (default for </P>7 |( @) I+ B" ^) w* m
<P>Microsoft Jet workspaces). </P>
! b$ @( v8 |6 d, t8 @<P>dbOptimistic?Uses optimistic locking to determine how changes are made to the </P>
& H9 u* f. }) j- d9 P0 B; ]<P>Recordset in a multiuser environment. The page containing the record is not </P>' T# p: C6 X( I0 ~
<P>locked until the Update method is executed. </P>
4 Q1 e, o4 S" W* r- Z" ?) |6 d. ~<P>dbOptimisticValue?Uses optimistic concurrency based on row values (ODBCDirect </P>
( |. [, Z& S7 M3 F5 G& S<P>workspaces only). </P>' i, ^, o0 ]" O) Z/ ~* [
<P>dbOptimisticBatch?Enables batch optimistic updating (ODBCDirect workspaces </P>
9 x3 z7 k; F* G<P>only). </P>
$ s. Z2 x# O) u7 \/ l! K. _<P>Remarks </P>7 V, {+ t% ~1 \4 J3 L) Z
<P>  </P>
- j$ O, u$ f" X8 {% o8 w( e<P>In a Microsoft Jet workspace, if object refers to a QueryDef object, or a </P>
5 S8 J0 N% R/ }! z. X# V  l<P>dynaset- or snapshot-type Recordset, or if source refers to an SQL statement </P>
( a1 f7 d  P( R! P( f<P>or a TableDef that represents a linked table, you can't use dbOpenTable for </P>/ ]& C1 Z/ c4 U$ b  L2 ?
<P>the type argument; if you do, a run-time error occurs. If you want to use an </P>8 C8 T+ y1 `' R! _- y2 _+ q
<P>SQL pass-through query on a linked table in a Microsoft Jet-connected ODBC </P>. d% y8 _- ?2 o  _8 ~0 u: e
<P>data source, you must first set the Connect property of the linked table's </P>
) b: ^* m+ K# N6 d, B( c0 x8 `8 q5 _<P>database to a valid ODBC connection string. If you only need to make a single </P>9 [  k$ h3 d3 ~2 h
<P>pass through a Recordset opened from a Microsoft Jet-connected ODBC data </P>
6 V8 u8 c: v- A' b<P>source, you can improve performance by using dbOpenForwardOnly for the type </P>; ?( ]6 S4 {" m. S, A: \( t
<P>argument. </P>7 o* Z2 s4 c) N
<P>  </P>
& N, A0 U+ L- e" e' s" q<P>If object refers to a dynaset- or snapshot-type Recordset, the new Recordset </P>0 @* }) e1 n) ?1 d* [0 ^
<P>is of the same type object. If object </P>( ?: S+ n. E0 ]' k, D6 n  `% O3 [
<P> refers to a table-type Recordset object, the type of the new object is a </P>
: e6 F1 i: b* b, R1 I) }<P>dynaset-type Recordset. You can't open new Recordset objects from forward-only </P>* U  r; t1 u+ T1 Q# ^
<P>杢ype or ODBCDirect Recordset objects. </P>
) y' q" |3 N: G$ C* Q' B) I+ U<P>In an ODBCDirect workspace, you can open a Recordset containing more than one </P>- z* Q3 [/ ^+ G- O2 c3 q
<P>select query in the source argument, such as </P>
4 F  i4 I, B+ a! b4 j<P>  </P>7 M) c2 q0 f& }& w0 L% G
<P>"SELECT LastName, FirstName FROM Authors </P>
% l1 C3 b" P$ F<P>WHERE LastName = 'Smith'; </P>
0 t! x7 U3 k2 ^# i<P>SELECT Title, ISBN FROM Titles </P>6 h+ S# A! z. l
<P>WHERE ISBN Like '1-55615-*'" </P>
- X& a5 q9 Y/ Y0 d/ R6 O<P>  </P>
" I3 v- F# E7 ~) c<P>The returned Recordset will open with the results of the first query. To </P>- X1 d9 C' P4 A" r2 S& F4 X8 b
<P>obtain the result sets of records from subsequent queries, use the </P>
( k$ U  D7 P4 ~6 n6 _0 P  W+ n<P>NextRecordset method. </P>
/ ~, T+ `/ P" m2 h& N: v<P>  </P>9 ?/ C  f# m+ @4 h) M! }- S3 x
<P>Note   You can send DAO queries to a variety of different database servers </P>
4 v, {4 S+ U. t: }+ @  M/ a<P>with ODBCDirect, and different servers will recognize slightly different </P>0 f8 Y9 o6 A4 ]  h4 k' A. W
<P>dialects of SQL. Therefore, context-sensitive Help is no longer provided for </P>
* n. i+ U* f. v0 Z1 j, \<P>Microsoft Jet SQL, although online Help for Microsoft Jet SQL is still </P>
) [3 R! E5 ?1 ~: L3 _& C( F! o, m5 {9 e<P>included through the Help menu. Be sure to check the appropriate reference </P>
) e/ R6 x4 j% Z4 y1 F<P>documentation for the SQL dialect of your database server when using either </P>
; P: M9 s  s8 y) q, C$ r8 H<P>ODBCDirect connections or pass-through queries in Microsoft Jet-connected </P>
0 g9 `7 d9 B/ h* ?& I<P>client/server applications. </P>; Y$ M. ^) l5 B
<P>  </P>
3 M; S8 i- {4 z, M' n<P>Use the dbSeeChanges constant in a Microsoft Jet workspace if you want to </P>! O$ H7 x& c! I: K; P
<P>trap changes while two or more users are editing or deleting the same record. </P>
' y$ E4 y- L" u$ S" ?9 x( o* U% R1 g<P>For example, if two users start editing the same record, the first user to </P>2 z0 }) w3 u! R# \, }
<P>execute the Update method succeeds. When the second user invokes the Update </P>
7 h& }) W. A* {/ Q& c8 B$ \) i5 L( L<P>method, a run-time error occurs. Similarly, if the second user tries to use </P>
& t6 K; h' f! g0 j<P>the Delete method to delete the record, and the first user has already </P>. D3 a' J$ n0 f$ ?( ^
<P>changed it, a run-time error occurs. </P>% t$ f& z2 V, [
<P>  </P>/ C2 Y* X) J/ z( @* T6 M
<P>Typically, if the user gets this error while updating a record, your code </P>& a; c; Q# [5 D% V0 z
<P>should refresh the contents of the fields and retrieve the newly modified </P>5 ?9 c: O) l6 Y, w9 Y+ s4 r
<P>values. If the error occurs while deleting a record, your code could display </P>/ D3 r% B% P, t# a! _
<P>the new record data to the user and a message indicating that the data has </P>5 h! e. e4 T, L
<P>recently changed. At this point, your code can request a confirmation that </P>4 k5 p0 W$ ?4 ]4 G5 ^$ E
<P>the user still wants to delete the record. </P>3 G; F" q% r0 @. [4 }
<P>  </P>
  p4 s* G+ P$ F  ~+ y, V4 U<P>You should also use the dbSeeChanges constant if you open a Recordset in a </P>
5 }" y/ i8 ?+ q6 Y0 Z<P>Microsoft Jet-connected ODBC workspace against a Microsoft SQL Server 6.0 (or </P>* i- Z0 h2 W0 Y+ k, W" C, y
<P>later) table that has an IDENTITY column, otherwise an error may result. </P>
/ a# M. T8 q, B- F8 k9 ^) [* ^<P>  </P>
2 I- O% p4 v6 d3 [0 T# j6 c<P>In an ODBCDirect workspace, you can execute asynchronous queries by setting </P>9 h7 B9 N: A1 ]5 g& r0 @+ T
<P>the dbRunAsync constant in the options argument. This allows your application </P>( t8 K' E7 M4 J! n
<P>to continue processing other statements while the query runs in the </P>6 x3 {. Y# a; @- Y, G
<P>background. But, you cannot access the Recordset data until the query has </P>; P; Z+ E4 a0 ^% ?3 ?% u/ f
<P>completed. To determine whether the query has finished executing, check the </P>
- x: @! C& S) K, e8 e- K- s7 S<P>StillExecuting property of the new Recordset. If the query takes longer to </P>( z& S, I! a: }/ _" q
<P>complete than you anticipated, you can terminate execution of the query with </P>" F; m% ?6 L0 U
<P>the Cancel method. </P>' M/ x' k6 Y7 v; V& R
<P>  </P>' `+ `# V+ U0 l4 S6 G3 h8 {2 x, W
<P>Opening more than one Recordset on an ODBC data source may fail because the </P>% Z2 J7 B0 ]4 u# |4 ^$ [
<P>connection is busy with a prior </P>
: [  g, I, i8 Y! B, U1 S7 r<P>OpenRecordset call. One way around this is to use a server-side cursor and </P>
# W5 o4 c/ l7 f3 j8 H9 S/ g<P>ODBCDirect, if the server supports this. Another solution is to fully </P>7 V1 k$ P$ z- m7 B9 {7 A3 h
<P>populate the Recordset by using the MoveLast method as soon as the Recordset </P>
- n6 U# y8 R: }6 S7 X2 R" E; P<P>is opened. </P>0 G5 e& r7 b4 h  G7 K+ q
<P>  </P>
. L5 p& |2 j" ?' N# P<P>If you open a Connection object with DefaultCursorDriver set to </P>
2 e, A/ w' v% j0 ?7 d$ P<P>dbUseClientBatchCursor, you can open a Recordset to cache changes to the data </P>
" Q! w: V7 ?( F1 ]& j5 g4 M0 A9 U<P>(known as batch updating) in an ODBCDirect workspace. Include dbOptimisticBatc </P>% u% x, Y1 _7 O3 P
<P>h in the lockedits argument to enable update caching. See the Update method </P>
3 [3 R# @  a9 F$ ?4 u( @4 _<P>topic for details about how to write changes to disk immediately, or to cache </P>
1 L0 Z, Z& r& k6 L! j) Z* B+ e<P>changes and write them to disk as a batch. </P>0 Z. J) K5 c! A: X7 t' ~2 `
<P>  </P>$ E+ [$ D7 E( n2 L. l
<P>Closing a Recordset with the Close method automatically deletes it from the </P>
' T' _% F# Y# N1 g( o" H
# M+ p5 u3 }& E- P<P>Recordsets collection. </P>! v2 x, P2 ~9 E# e: y9 l" u
<P>  </P>, A! s1 u7 j: b( o
<P>Note   If source refers to an SQL statement composed of a string concatenated </P>, G/ t: g+ x" H; r5 c/ ^- S
<P>with a non-integer value, and the system parameters specify a non-U.S. </P>' @* m7 q2 b1 g; P' u
<P>decimal character such as a comma (for example, strSQL = "PRICE &gt; " &amp; </P>- k5 |6 k4 j8 z6 A
<P>lngPrice, and lngPrice = 125,50), an error occurs when you try to open the </P>' ], e8 Q' K/ n. l) I
<P>Recordset. This is because during concatenation, the number will be converted </P>- f: n* T/ w0 @6 y# D
<P>to a string using your system's default decimal character, and SQL only </P>
. |" e( m$ n; J; A5 a" D<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-16 05:33 , Processed in 0.394108 second(s), 52 queries .

回顶部