VB6 ADODB.Recordset RecordCount property always returns -1

前端 未结 12 1799
别跟我提以往
别跟我提以往 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:35

    You may try something like this..

    Set rs = mCmd.Execute
    
    rs.MoveFirst
    
    Do Until rs.EOF = true
    
        Debug.Print rs.RecordCount  ' Always returns -1  !!
        Debug.Print rs.Fields(0)   ' returns correct data for first row, first col
        Debug.Print rs.Fields(1)   ' returns correct data for first row, 2nd col
        Debug.Print rs.Fields(2)   ' returns correct data for first row, 3rd col
    
       counter = counter + 1
       rs.MoveNext
    
    Loop
    
    0 讨论(0)
  • 2020-12-11 16:36

    Actually the CursorLocation plays a major role in this case. Use rs.CursorLocation = adUseClient to set the cursor location and try.

        Set rs = New ADODB.Recordset
        rs.CursorLocation = adUseClient
        Dim DbConnectionString As String
    
        DbConnectionString = mSqlProvider & _
                                mSqlHost
    
    
        Set mDbConnection = New ADODB.Connection
        mDbConnection.CursorLocation = adUseServer
    
        Call mDbConnection.Open(DbConnectionString)
    
        If mDbConnection.State = adStateOpen Then
            Debug.Print (" Database is open")
            ' Initialise the command object
            Set mCmd = New ADODB.Command
            mCmd.ActiveConnection = mDbConnection
    
            mCmd.CommandText = "select * from myTestTable"
            mCmd.CommandType = adCmdText
    
            Set rs = mCmd.Execute
    
            Debug.Print rs.RecordCount  ' This should now return the right value.
            Debug.Print rs.Fields(0)   ' returns correct data for first row, first col
            Debug.Print rs.Fields(1)   ' returns correct data for first row, 2nd col
            Debug.Print rs.Fields(2)   ' returns correct data for first row, 3rd col
    
        End If
    
    End Sub
    
    0 讨论(0)
  • 2020-12-11 16:38

    That's a result of the type of cursor used to access the data, this post covers the issue and possible fixes.

    http://www.devx.com/tips/Tip/14143

    EDIT

    I apologize for not being more attentive to the fact that you were dealing with Compact. With Compact the situation is similar to the one I referenced, as it uses forward only cursors by default (which do not support row count) but there are two other cursor types available as documented in the link below.

    http://support.microsoft.com/kb/272067

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

    Here is a solution for you that I used

    Dim recordnumber As Long
    Dim SalRSrec As New ADODB.Recordset
    Set SalRSrec = Nothing
    SalRSrec.Open ("SELECT count(*) from SALARY where EMPID= '" & cmb_empid & "' ;"), Dbase, adOpenKeyset, adLockOptimistic
    recordnumber = SalRSrec.GetString
    MsgBox recordnumber
    
    0 讨论(0)
  • 2020-12-11 16:39

    Below code might help you,

    set conn = CreateObject("ADODB.Connection") 
    conn.open "<connection string>" 
    set rs = CreateObject("ADODB.Recordset") 
    sql = "SELECT columns FROM table WHERE [...]" 
    rs.open sql,conn,1,1 
    if not rs.eof then 
        nr = rs.recordcount 
        response.write "There were " & nr & " matches." 
        ' ... process real results here ... 
    else 
        response.write "No matches." 
    end if 
    rs.close: set rs = nothing 
    conn.close: set conn = nothing 
    
    0 讨论(0)
  • 2020-12-11 16:41

    Replace Set rs = mCmd.Execute with:

    set rs = new ADODB.Recordset
    rs.Open "select * from myTestTable", mDBConnection, adOpenDynamic, adLockOptimistic
    

    The adOpenDynamic will allow a forward/backward read through to get your recordcount.

    0 讨论(0)
提交回复
热议问题