ADO and OLE DB map cursors over the result sets of executed SQL statements. SQLOLEDB implements these operations through the use of server cursors, which are cursors implemented on the server and managed by API cursor functions.
By default, an ADO application does not use Microsoft® SQL Server™ API server cursors with SQLOLEDB. The default cursor used by the ADO application is read-only and forward-only and uses default result set processing.
Because ADO allows the setting of cursor properties, a number of options exist for using cursors with ADO and SQL Server:
If you use these settings, the provider uses default result set processing (forward only and read-only cursor). The default settings allow a program to execute any Transact-SQL statement; however, only one statement can be active on any connection at a time. The program must either fetch all the rows or cancel the result set before another statement can be executed on the same connection. Following those rules, a program can process Transact-SQL statements or stored procedures that allow multiple result sets.
The provider then uses SQL Server API server cursors to deliver the requested cursor functionality. Although this option provides a wide range of cursor functionality, it introduces some restrictions. For example, you cannot execute any Transact-SQL statement, batch, or stored procedure that returns more than a single result set. However, it is possible to have multiple active statements on a single connection (meaning that there can be pending results in the statement handle), provided they are all executed with API server cursors.
In this case, ADO implements the cursor; therefore, the application can use only the capabilities supported by the ADO client cursors. The application cannot access the cursor capabilities of the underlying provider. Only a CursorType property of adOpenStatic (static cursor) is supported for a setting of adUseClient.
An ADO application can control the cursor functionality through the following Recordset properties.
Property | Description |
---|---|
CursorType | Default: adOpenForwardOnly Indicates the type of cursor used: Forward-only/read-only (adOpenForwardOnly) Static (adOpenStatic) Keyset (adOpenKeyset) Dynamic (adOpenDynamic) |
CursorLocation | Default: adUseServer Sets or returns the location of the cursor engine. If you set this property to adUseClient, you can only open a static cursor. |
LockType | Default: adLockReadOnly Indicates the type of locks placed on records during editing. |
CacheSize | Default: 1 Controls how many records the provider keeps in its buffer and how many to retrieve at one time into local memory. |
To use a server cursor, an application can set these properties to anything other than the default value:
The CursorLocation property should be left at adUseServer, the default setting.
Server cursors are only created for statements that begin with:
Even if an application explicitly requests a server cursor, server cursors are not created for statements such as INSERT.
Server cursors also cannot be used with statements that generate more than one recordset.
This restriction applies to all statements described in Generating Multiple Recordsets. If a server cursor is used with any statement that generates multiple recordsets, an application can get one of the following errors:
This example shows the opening of a dynamic server cursor:
Dim rs As New ADODB.Recordset
. . .
rs.Open "SELECT * FROM titles", , adOpenDynamic, adLockOptimistic
rs.Close
API Server Cursors | Default Result Sets |
Generating Multiple Recordsets |