VB6 ADODB.Recordset RecordCount property always returns -1

前端 未结 12 1798
别跟我提以往
别跟我提以往 2020-12-11 15:43

I am trying to get some old VB6 code to work with SQL Server Compact.

I can connect, open the database and all seems well. I can run insert select commands which

相关标签:
12条回答
  • 2020-12-11 16:16

    With Compact the default cursor attribute is adOpenForwardOnly for improved performance. As such RecordCount is returned as "-1" which means its not available, rather than blank. This is by design because the # of records in a dynamic cursor could change and result in pinging back and forth between the client server to maintain accuracy. However, if the record count is vital try setting it to use adOpenKeyset or adOpenStatic with a server-side cursor.

    0 讨论(0)
  • 2020-12-11 16:17
    Set cn = CreateObject("ADODB.Connection")
    
    strVerb = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
        "Data Source=C:\test.accdb"
    
    tab1 = "tabelle1"
    
    strSQL = "SELECT Count(*) FROM " & tab1
    
    Debug.Print strSQL
             
    cn.Open strVerb
    Set rs = cn.Execute(strSQL)
    
    Debug.Print rs.Fields(0)
        
    rs.Close
    Set rs = Nothing
    cn.Close
    Set cn = Nothing
    
    0 讨论(0)
  • 2020-12-11 16:21

    Check Recordset Property

    The follow is result that RecordCount value returned by com.status.live code

    +------------------+-------------------+-------------+---------------+--------------+
    |    CursorTypeEnum|adOpenForwardOnly=0|dOpenKeyset=1|adOpenDynamic=2|adOpenStatic=3|
    |CursorLocationEnum|                                                                |
    +------------------+-------------------+-------------+---------------+--------------+
    |adUseServer = 2   |         X         |      O      |       X       |       O      |
    |adUseClient = 3   |         O         |      O      |       O       |       O      |
    +------------------+-------------------+-------------+---------------+--------------+
    
    0 讨论(0)
  • 2020-12-11 16:26

    From memory with working with VB6/ADO a long time ago the .RecordCount field doesn't return meaningful data until you've moved to the end of the recordset.

    rs.MoveLast
    rs.MoveFirst
    Debug.Print rs.RecordCount
    

    Though with this you'll need to make sure you have the appropriate cursor type (i.e., not forward only).

    The only other solution I can think of is to do a separate SELECT COUNT(*) FROM myTestTable, etc but this has issues with the data changing between that call, and the one that actually returns the rows.

    0 讨论(0)
  • 2020-12-11 16:27

    Try using following code if still returns -1

    Set Conn = createobject("ADODB.connection")
    Set Rs = createobject("ADODB.recordset")
    Conn.Open "DSN=DSN_QTP" 
    'Rs.Open "Select * From orders",Conn,adOpenDynamic,adLockBatchOptimistic
    Rs.Open "Select * from [QTP-Table]",Conn,1 'Use either 1 or 3
    'I tried using adopendynamic but it still returned -1. Using 1 it gave me correct count.       'Though I am using this code in QTP (Vbscript) same should work for VB6 also.
    msgbox Rs.RecordCount
    
    0 讨论(0)
  • 2020-12-11 16:31

    This following code returns the recortcount exactly...

    Public Sub test()
        Dim cn As New ADODB.Connection()
        Dim sPath As String = Application.ExecutablePath
        sPath = System.IO.Path.GetDirectoryName(sPath)
    
        If sPath.EndsWith("\bin") Then
            sPath = sPath.Substring(0, Len(sPath) - 4)
        End If
    
        Dim DbConnectionString As String
        DbConnectionString = "provider=microsoft.jet.oledb.4.0;data source=" & sPath & "\students.mdb"
    
        cn.ConnectionString = DbConnectionString
        cn.Open()
    
        Dim rs As New ADODB.Recordset()
        rs.CursorLocation = ADODB.CursorLocationEnum.adUseClient
        rs.CursorType = ADODB.CursorTypeEnum.adOpenStatic
        rs.LockType = ADODB.LockTypeEnum.adLockBatchOptimistic
        rs.Open("select * from students", cn)
        MsgBox(rs.RecordCount)
    
        rs.ActiveConnection = Nothing
        cn.Close()
    End Sub
    
    0 讨论(0)
提交回复
热议问题