ado的recordset的recordcount属性总是为-1.txt

来自「以电子书的形式收集了VB一些常见问题解决方法,可以很方便的查找自己需要解决的问题」· 文本 代码 · 共 53 行

TXT
53
字号
SYMPTOMS 
    ======== 
     
    When you request the RecordCount for a serverside recordset, a -1 may return. This occurs with ActiveX Data Objects (ADO) version 2.0 when the CursorType is adOpenForwardonly or adOpenDynamic. This occurs with ADO 1.5 only when the cursortype is adOpenForwardonly. Testing with the OLEDB provider for JET and SQL Server produces varying results, depending on the provider. 
     
    Providers may not support some CursorTypes. When you select a CursorType that is not supported, the provider should select the CursorType closest to what you request. Please refer to the documentation that comes with your provider. Also, please note that not all combinations of LockType and CursorType work together. Changing a LockType may force a change in the CursorType. Be sure to use debug to check the value of CursorType when conducting tests with your OLEDB provider. 
     
    CAUSE 
    ===== 
     
    The number of records in a dynamic cursor may change. Forward only cursors do not return a RecordCount. 
     
    RESOLUTION 
    ========== 
     
    Use either adOpenKeyset or adOpenStatic as the CursorType for server side cursors or use a client side cursor. Client side cursors use only adOpenStatic for CursorTypes regardless of which CursorType you select. 
     
    STATUS 
    ====== 
     
    This behavior is by design. 
     
    MORE INFORMATION 
    ================ 
     
    Steps to Reproduce Behavior 
    --------------------------- 
     
    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:\vb6_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 count. 
     
     4. Change the CursorLocation to adUseClient and experiment with the different CursorTypes. In all cases the correct record count returns. 
<END>    
因为在c/s结构中,记录集是分页存储的,当你从服务器请求数据时,它不会给你全部数据。最好用rs.getrows()的到所有记录行 
<END>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?