The RecordCount property of ADO's Recordset is always -1

xiaoxiao2021-03-06  15

Phenomenon ==== When the server is requested to request RecordCoun, -1 is returned. This is because Cursortype in ActiveX Data Objects (ADO) 2.0 is AdopenForwardOnly or AdoPendynamic. If it is ADO 1.5, only at Cursortype is AdopenForwardonly. If the results generated using OLEDB Provider for Jet and SQL Server may be different, this depends on the provider of the database. The provider may not support certain Cursortypes. When your CURSORTYPE is not supported, the provider will choose the Cursortype you requested. Please refer to your provider's documentation. Also note that all the combinations of LockType and Cursortype can work. Changing LockType will force Cursortype. Please make sure to use debugging to check the value of the CURSORTYPE. The reason ===== The record number may change in the dynamic cursor. The word ONLY cursor cannot return RecordCount. Solution ========== Use adopenkeyset (= 1) or adopenStatic (= 3) as a server-side cursor or client game. The client only uses AdoPenStatic as Cursortypes, regardless of what kind of Cursortype you choose. State ====== This form is designed.

More information ================ Repeat behavior steps ------------------------- 1. Open a standard .exe project in Visual Basic From the Project menu, choose References Select either the Microsoft Active Data Object 1.5 Library or the Microsoft Active Data Object 2.0 Library 2. Paste the following code in the form code window...: Option Explicit Dim rs As ADODB.Recordset Private Sub Form_Load () 'set up rs Set rs = New ADODB.Recordset rs.CursorLocation = adUseServer rs.Open "Select ProductID from products", & _ "Provider = Microsoft.Jet.OLEDB. 3.51; "& _" Data Source = d: /vb5_win95/nwind.mdb ", _ adOpenDynamic, adLockUnspecified Debug.Print rs.RecordCount End Sub 3. Replace the preceding Data Source with a Data Source on your computer Run the preceding form. And Note The Record Count. Change The Cursortype To AdopenForwardOnly and Note The Record. 4. Change The CursorLocation TO A Duseclient and experiment with the DiffERENT CURSORTYPES. IN All Cases The Correct Record Count Returns. ====================

note

Cursortype properties

Setting and return value

Set or return to the following CURSORTYPEENUM value.

Constant Description AdopenForwardOnly is only a forward cursor, the default value. In addition to only rolling forward in the record, it is the same as the static cursor. When only one-way movement is moved in the recordset, use it improves performance. AdoPenKeyset key set cursor. Although the records from your record set cannot access other users, the key set cursor is similar to the dynamic cursor unable to see the records added by other users. Other users can still change the data. ADOPENDYNAMIC Dynamic Cursor. You can see the addition, changes, and delete of other users. Allows all types of movements in the recordset, but does not include bookmark operations that the provider does not support. AdoPenStatic static cursor. You can use a static copy of the record collection of data or generating a report. In addition, it is not visible to the addition, changes or deletions made by other users.

Description

Use the CURSORTYPE attribute to specify the type of cursor that should be used when opening the Recordset object. The CURSORTYPE attribute is read / write when RecordSet is closed, and the property is read-only when the RecordSet is open. If you set the CursorLocation property to AduseClient, you only support the settings of the ADUSSTATIC. If the unsupported value is set, it will not cause errors, which will use the closest supported Cursortype.

If the provider does not support the requested cursor type, the provider may return other cursor types. When you open the Recordset object, change the CURSORTYPE attribute to match the cursor actually used. To verify the specified feature of the returned cursor, use the Supports method. After turning off the Recordset, the CURSORTYPE attribute will be restored to the original setting.

The following table describes the provider features required for each cursor type (identified by the Supports method).

For this a Recordset CursorType for all these constants, Supports must return TrueAdOpenForwardOnly no AdOpenKeysetAdBookmark, adHoldRecords, adMovePrevious, adResyncAdOpenDynamicadMovePreviousAdOpenStaticadBookmark, adHoldRecords, adMovePrevious, adResync

Note that although the dynamic cursor and the forward cursor supports may be true, the batch cursor or static cursor should be used for batch updates. Set the LockType property to AdlockBatchOptiMistic, and then set the CursorLocation property to AduseClient to enable the OLE DB cursor service you need to rendering the rendering.

Remote Data Services When used for Client (Ador) Recordset objects, you can only set the Cursortype property to AdoPenStatic.

CursorLocation attribute

Set or return to the location of the cursor service.

Setting and return value

Set or returns a long integer value that can be set to a certain constant below.

Constant Description Adusenone does not use cursor services. (This constant is out of time and only in order to compatibility). AduseClient uses client cursors provided by the local bid library. Many of the local cursor services typically allow for use may be that the cursors provided by the driver cannot be used, so use this setting for those who will enable. AduseClient has backward compatibility and also supports synonymous aduseclientbatch. The ADUSERVER defaults. Use the data provider or the cursor provided by the driver. These cursors are sometimes very flexible, and other users have additional sensitivity to changes in data sources. However, some features of Microsoft Client Cursor Provider (such as disconnected records) cannot be simulated by the server-side game, which will not be used by this setting.

Description

This attribute allows you to choose in various cursors available for the provider. Typically, you can choose to use a client game library or a bike library on the server.

This property setting has an impact on the connection established after the attribute has been set. Changing the CursorLocation property does not affect existing connections.

For the Connection or the closed Recordset this property is read / write, and the open RECORDSET is read-only.

The cursor returned by the Execute method inherits this setting. Recordset will automatically inherit this setting from the connection associated with it.

Remote Data Services When used in the client (Ador) Recordset or Connection object, you can only set the CursorLocation property to aduseclient.

The LockType property indicates the type of lock used during the editing process.

Setting and return value

Set or returns the value of a LockTyPeenum below.

Constant illustrates the ADLOCKREADONLY default value, read only. Unable to change the data. AdlockPESSIMISTIC conservative record lock (strip). The provider performs the necessary operations to ensure successful editing records, usually using the record of the data source immediately when editing. AdlockOptimistic open record lock (strip). The provider uses an open lock to lock the record only when the Update method is called. AdlockBatchOptimistic open batch update. The batch update mode is used to update now.

Description

Setting the LockType property before opening RecordSet Specifies the lock type that the provider should use when the Open is open. Read this property returns the type of lock that is using on the open Recordset object. The LockType property is read / written when Recordset is closed. This property is read-only.

The provider may not support all lock types. If a provider does not support the required LockType settings, it will be replaced with other types of locks. To determine the actual lock function available for the Recordset object, use the Supports method with AduPdate and AduPdateBatch.

If the CursorLocation property is set to AduseClient, AdlockPessimistic settings will not be supported. Settings that are not supported do not generate errors because the value of the closest LockType will be used at this time.

Remote Data Services When used on the client (Ador) RecordSet object, the LockType property can only be set to AdlockOptimisticbatch.

转载请注明原文地址:https://www.9cbs.com/read-46982.html

New Post(0)