Click here to Skip to main content
15,890,973 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 work.

However the ADODB.Recordset RecordCount property always returns -1 even though I can access the Fields and see the data. Changing the CursorLocation = adUseClient causes a problem when executung the SQL (multiple-step operation generated errors).

    Option Explicit
Private Const mSqlProvider As String = "Provider=Microsoft.SQLSERVER.CE.OLEDB.3.5;"
    Private Const mSqlHost              As String = "Data Source=C:\Database.sdf;"
    Private mCmd                        As ADODB.Command   ' For executing SQL
    Private mDbConnection               As ADODB.Connection


Private Sub Command1_Click()


   Dim rs As ADODB.Recordset

    Set rs = New ADODB.Recordset


    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  ' 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

    End If

End Sub


Any advice would be gratefully accepted.

Thank you

*repost*
Posted

Set the cursor & lock type to get the recordcount
VB
objRS.Open strSQL, objConn, adOpenStatic, adLockReadOnly, adCmdText

'Or..
rs.CursorLocation = ADODB.CursorLocationEnum.adUseClient     
rs.CursorType = ADODB.CursorTypeEnum.adOpenStatic     
rs.LockType = ADODB.LockTypeEnum.adLockBatchOptimistic 
 
Share this answer
 
Comments
agent_suwastica 12-Jan-12 0:53am    
Still the recordcount property returns -1 value.
I see the cursorlocation property has been reset to 2, and cursortype has been reset to 0 after the recordset had reset..
agent_suwastica 12-Jan-12 0:53am    
i have to use: set rs = cmd.execute
Om Prakash Pant 12-Jan-12 6:57am    
i am not sure you will get count by setting the rs. pl see the following link for options:
http://www.devx.com/tips/Tip/14143
I solve it by creating an stored procedure with multiple recordset and call it by adodb.command
I use
RS.NextRecordset
to get the next recordset :)

anyway, my second recordset is "Select @@rowcount as recordCount"
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900