gpt4 book ai didi

sql-server - VB6 ADODB.Recordset RecordCount 属性总是返回 -1

转载 作者:太空狗 更新时间:2023-10-30 01:44:41 25 4
gpt4 key购买 nike

我正在尝试让一些旧的 VB6 代码与 SQL Server Compact 一起使用。

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

但是 ADODB.Recordset RecordCount 属性始终返回 -1,即使我可以访问字段并查看数据。更改 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

我们将不胜感激地接受任何建议。

最佳答案

实际上 CursorLocation 在这种情况下起着重要作用。使用 rs.CursorLocation = adUseClient 设置光标位置并尝试。

    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

关于sql-server - VB6 ADODB.Recordset RecordCount 属性总是返回 -1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2282661/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com