sql-server – VB6 ADODB.Recordset RecordCount属性始终返回-1

前端之家收集整理的这篇文章主要介绍了sql-server – VB6 ADODB.Recordset RecordCount属性始终返回-1前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图让一些旧的VB6代码sql Server Compact一起使用.

我可以连接,打开数据库,一切似乎都很好.我可以运行插入选择命令.

但是,ADODB.Recordset RecordCount属性始终返回-1,即使我可以访问Fields并查看数据.更改CursorLocation = adUseClient会在执行sql(多步操作生成错误)时导致问题.

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

任何建议都会被感激地接受.

解决方法

这是用于访问数据的游标类型的结果,这篇文章涵盖了问题和可能的修复.

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

编辑

我为没有更加关注你正在处理Compact这一事实而道歉.使用Compact时,情况类似于我引用的情况,因为它默认使用仅向前游标(不支持行计数),但是有两种其他游标类型可用,如下面的链接中所述.

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

猜你在找的MsSQL相关文章