gpt4 book ai didi

excel - 记录集始终返回-1(excel VBA)

转载 作者:行者123 更新时间:2023-12-01 19:04:33 26 4
gpt4 key购买 nike

我想计算记录集中查询返回的行数,我尝试了 recset.RecordCount 函数,但它总是返回 -1 值。

如何计算记录集中的记录数或行数?

最佳答案

在连接对象中指定参数很重要:CursorLocation = adUseClient。

dbName = "DbInjectorsCatalog"
dbFilePath = "C:\DbInjectorsCatalog.mdf"

connStr = "Driver={SQL Server native Client 11.0};" & _
"Server=(LocalDB)\v11.0;" & _
"AttachDBFileName=" & dbFilePath & ";" & _
"Database=" & dbName & ";" & _
"Trusted_Connection=Yes"
sqlStr = "Select * from Injectors"

Set conn = New ADODB.Connection
conn.ConnectionString = connStr

'>>> scpecify cursor location
conn.CursorLocation = adUseClient

conn.Open
Set rs = New ADODB.Recordset
rs.Open sqlStr, connStr, adOpenStatic, adLockBatchOptimistic

完整的工作示例在这里: http://straightitsolutions.blogspot.com/2014/12/read-recordcount-from-adodbrecordset.html

关于excel - 记录集始终返回-1(excel VBA),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20676416/

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