Open 方法(ADO 记录集)Open Method (ADO Recordset)

打开 记录集 对象上的游标。Opens a cursor on a Recordset object.


recordset.Open Source, ActiveConnection, CursorType, LockType, Options  


可选。Optional. 一个 变量,该变量的计算结果为有效的 命令对象、SQL 语句、表名、存储过程调用、URL 或包含持久存储的 记录集的文件或 对象的名称。A Variant that evaluates to a valid Command object, an SQL statement, a table name, a stored procedure call, a URL, or the name of a file or Stream object containing a persistently stored Recordset.

可选。Optional. 计算结果为有效的 连接对象变量名称的 变量,或包含 ConnectionString参数的 字符串Either a Variant that evaluates to a valid Connection object variable name, or a String that contains ConnectionString parameters.

可选。Optional. 一个 CursorTypeEnum 值,确定提供程序在打开 记录集 时应使用的游标类型。A CursorTypeEnum value that determines the type of cursor that the provider should use when opening the Recordset. 默认值为 adOpenForwardOnlyThe default value is adOpenForwardOnly.

可选。Optional. LockTypeEnum值,用于确定提供程序在打开 记录集 时应使用的 (并发) 类型。A LockTypeEnum value that determines what type of locking (concurrency) the provider should use when opening the Recordset. 默认值为 adLockReadOnlyThe default value is adLockReadOnly.

可选。Optional. 一个 长整型 值,该值指示当提供程序表示某个 命令 对象以外的内容时,该提供程序应如何计算 Source 参数,或者应从以前保存该记录集的文件还原该 记录集A Long value that indicates how the provider should evaluate the Source argument if it represents something other than a Command object, or that the Recordset should be restored from a file where it was previously saved. 可以是一个或多个 CommandTypeEnumExecuteOptionEnum 值,可以与按位 or 运算符组合。Can be one or more CommandTypeEnum or ExecuteOptionEnum values, which can be combined with a bitwise OR operator.


如果从包含持久 记录集 打开 记录集,则使用 adAsyncFetchNonBlockingExecuteOptionEnum值将不起任何作用;提取将是同步和阻塞。If you open a Recordset from a Stream containing a persisted Recordset, using an ExecuteOptionEnum value of adAsyncFetchNonBlocking will have no effect; the fetch will be synchronous and blocking.


AdExecuteNoRecordsadExecuteStreamExecuteOpenEnum 值不应与 Open 一起使用。The ExecuteOpenEnum values of adExecuteNoRecords or adExecuteStream should not be used with Open.


ADO 记录集 的默认游标是位于服务器上的只进只读游标。The default cursor for an ADO Recordset is a forward-only, read-only cursor located on the server.

Recordset 对象使用 Open 方法将打开一个游标,该游标表示基表、查询结果或以前保存的 记录集 的记录。Using the Open method on a Recordset object opens a cursor that represents records from a base table, the results of a query, or a previously saved Recordset.

使用可选的 Source 自变量来指定数据源,使用以下项之一: 命令 对象变量、SQL 语句、存储过程、表名称、URL 或完整的文件路径名称。Use the optional Source argument to specify a data source using one of the following: a Command object variable, an SQL statement, a stored procedure, a table name, a URL, or a complete file path name. 如果 是文件路径名称,则它可以是 ( "c:\dir\file.rst" ) 的完整路径, ( "。\file.rst ") 或 () 的 URL https://files/file.rstIf Source is a file path name, it can be a full path ("c:\dir\file.rst"), a relative path ("..\file.rst"), or a URL (https://files/file.rst).

使用 Open 方法的 Source 参数执行不返回记录的操作查询是一个不错的做法,因为没有简单的方法来确定调用是否成功。It is not a good idea to use the Source argument of the Open method to perform an action query that does not return records because there is no easy way to determine whether the call succeeded. 此类查询返回的 记录集 将关闭。The Recordset returned by such a query will be closed. 若要执行不返回记录的查询(如 SQL INSERT 语句),请改为调用 命令 对象的 Execute方法或 连接对象的 execute方法。To perform a query that does not return records, such as a SQL INSERT statement, call the Execute method of a Command object or the Execute method of a Connection object instead.

ActiveConnection 参数对应于 ActiveConnection属性,并指定打开 Recordset 对象的连接。The ActiveConnection argument corresponds to the ActiveConnection property and specifies in which connection to open the Recordset object. 如果传递此参数的连接定义,ADO 将使用指定的参数打开新连接。If you pass a connection definition for this argument, ADO opens a new connection using the specified parameters. 通过将 CursorLocation属性设置为 adUseClient,打开包含客户端游标的 记录集 后,可以更改此属性的值以将更新发送到另一个提供程序。After you open the Recordset with a client-side cursor by setting the CursorLocation property to adUseClient, you can change the value of this property to send updates to another provider. 或者,你可以在 Microsoft Visual Basic) 中将此属性设置为 " ( ",或者为 NULL,以便从任何提供程序断开 记录集 的连接。Or you can set this property to Nothing (in Microsoft Visual Basic) or NULL to disconnect the Recordset from any provider. 但更改服务器端游标的 ActiveConnection 会生成错误。Changing ActiveConnection for a server-side cursor generates an error, however.

对于直接与 Recordset 对象的属性相对应的其他参数 (CursorTypeLockType) ,属性的参数关系如下:For the other arguments that correspond directly to properties of a Recordset object (Source, CursorType, and LockType), the relationship of the arguments to the properties is as follows:

  • 在打开 Recordset 对象之前,该属性是可读/写的。The property is read/write before the Recordset object is opened.

  • 除非在执行 Open 方法时传递相应的参数,否则将使用属性设置。The property settings are used unless you pass the corresponding arguments when executing the Open method. 如果传递参数,它将重写相应的属性设置,并使用参数值更新属性设置。If you pass an argument, it overrides the corresponding property setting, and the property setting is updated with the argument value.

  • 打开 Recordset 对象之后,这些属性将变为只读。After you open the Recordset object, these properties become read-only.


对于 Source属性设置为有效 命令 对象的 recordset 对象, ActiveConnection 属性是只读的,即使 recordset 对象未打开也是如此。The ActiveConnection property is read-only for Recordset objects whose Source property is set to a valid Command object, even if the Recordset object is not open.

如果在 Source 参数中传递 Command 对象并同时传递 ActiveConnection 参数,则会发生错误。If you pass a Command object in the Source argument and also pass an ActiveConnection argument, an error occurs. Command 对象的 ActiveConnection 属性必须已设置为有效的 连接 对象或连接字符串。The ActiveConnection property of the Command object must already be set to a valid Connection object or connection string.

如果在 source 参数中传递除 命令 对象之外的内容,则可以使用 Options 参数优化 source 参数的计算。If you pass something other than a Command object in the Source argument, you can use the Options argument to optimize evaluation of the Source argument. 如果未定义 Options 参数,则可能会遇到性能降低的情况,因为 ADO 必须对提供程序进行调用,以确定参数是 SQL 语句、存储过程、URL 还是表名称。If the Options argument is not defined, you may experience diminished performance because ADO must make calls to the provider to determine if the argument is an SQL statement, a stored procedure, a URL, or a table name. 如果知道所使用的 类型,设置 Options 参数会指示 ADO 直接跳转到相关代码。If you know what Source type you are using, setting the Options argument instructs ADO to jump directly to the relevant code. 如果 Options 参数与 类型不匹配,则会发生错误。If the Options argument does not match the Source type, an error occurs.

如果在 Source 参数中传递 Stream 对象,则不应将信息传递到其他参数。If you pass a Stream object in the Source argument, you should not pass information into the other arguments. 这样做会导致错误生成。Doing so will generate an error. 中打开 记录集 时,不会保留 ActiveConnection 信息。The ActiveConnection information is not retained when a Recordset is opened from a Stream.

如果没有与 记录集 相关联的连接,则 Options 参数的默认值为 adCmdFileThe default for the Options argument is adCmdFile if no connection is associated with the Recordset. 这通常是永久存储的 记录集 对象的事例。This will typically be the case for persistently stored Recordset objects.

如果数据源未返回任何记录,则提供程序会将 BOFEOF 属性都设置为 True,并且不定义当前记录位置。If the data source returns no records, the provider sets both the BOF and EOF properties to True, and the current record position is undefined. 如果游标类型允许,你仍可以向此空 Recordset 对象添加新数据。You can still add new data to this empty Recordset object if the cursor type allows it.

对打开的 记录集 对象结束操作后,请使用 Close 方法释放任何关联的系统资源。When you have concluded your operations over an open Recordset object, use the Close method to free any associated system resources. 关闭对象并不会将其从内存中删除;您可以更改其属性设置,并使用 open 方法稍后再次打开它。Closing an object does not remove it from memory; you can change its property settings and use the Open method to open it again later. 若要从内存中完全消除对象,请将对象变量设置为 NothingTo completely eliminate an object from memory, set the object variable to Nothing.

设置 ActiveConnection 属性之前,请调用 Open with no 操作数,以创建通过向 recordset 字段集合追加字段而创建的 记录集 的实例。Before the ActiveConnection property is set, call Open with no operands to create an instance of a Recordset created by appending fields to the Recordset Fields collection.

如果已将 CursorLocation 属性设置为 adUseClient,则可以通过以下两种方式之一异步检索行。If you have set the CursorLocation property to adUseClient, you can retrieve rows asynchronously in one of two ways. 推荐的方法是将 选项 设置为 adAsyncFetchThe recommended method is to set Options to adAsyncFetch. 或者,您可以使用 Properties 集合中的 "异步行集处理" 动态属性,但如果不将 Options 参数设置为 adAsyncFetch,则相关的检索事件可能会丢失。Alternatively, you can use the "Asynchronous Rowset Processing" dynamic property in the Properties collection, but related retrieved events can be lost if you do not set the Options parameter to adAsyncFetch.


仅通过 Open 方法的 OPTIONS 参数支持 MS 远程访问接口中的后台获取。Background fetching in the MS Remote provider is supported only through the Open method's Options parameter.


使用 http 方案的 Url 将自动调用 用于 Internet 发布的 Microsoft OLE DB 提供程序URLs using the http scheme will automatically invoke the Microsoft OLE DB Provider for Internet Publishing. 有关详细信息,请参阅 绝对和相对 urlFor more information, see Absolute and Relative URLs.

CommandTypeEnumExecuteOptionEnum值的某些组合无效。Certain combinations of CommandTypeEnum and ExecuteOptionEnum values are not valid. 有关无法组合的选项的信息,请参阅 ExecuteOptionEnumCommandTypeEnum的主题。For information about which options cannot be combined, see the topics for the ExecuteOptionEnum, and CommandTypeEnum.

应用于Applies To

记录集对象 (ADO)Recordset Object (ADO)

另请参阅See Also

(VB) 的打开和关闭方法示例 Open and Close Methods Example (VB)
(VBScript) 的打开和关闭方法示例 Open and Close Methods Example (VBScript)
打开和关闭方法示例 (VC + +) Open and Close Methods Example (VC++)
(VB 保存和打开方法示例) Save and Open Methods Example (VB)
开放式方法 (ADO 连接) Open Method (ADO Connection)
(ADO 记录的 Open 方法) Open Method (ADO Record)
ADO 流 (打开方法) Open Method (ADO Stream)
OpenSchema 方法 OpenSchema Method
Save 方法Save Method