[英]ADODB RecordSet RecordCount doesn't return useful information
I have a pretty basic execution of a stored procedure in my code: 我在代码中对存储过程进行了非常基本的执行:
Dim objCmd As ADODB.Command
Dim objConn As ADODB.Connection
Dim rsTemplate As ADODB.Recordset
Set objConn = New ADODB.Connection
objConn.Open strConnection
Set objCmd = New ADODB.Command
objCmd.ActiveConnection = objConn
objCmd.CommandType = adCmdStoredProc
objCmd.CommandText = "GetTemplate"
objCmd.Parameters.Append objCmd.CreateParameter("@Param1", adInteger, adParamInput, , lngParam1)
objCmd.Parameters.Append objCmd.CreateParameter("@Param2", adInteger, adParamInput, , lngParam1)
objCmd.CommandTimeout = 600
Set rsTemplate = objCmd.Execute()
LogInfo "Found " & rsTemplate.RecordCount & " templates"
The SP is supposed to find a single row. SP应该找到单个行。 The problem is that sometimes it fines none, which is actually okay, except that the
RecordCount
property is -1 in both cases. 问题是,有时它不执行任何操作,实际上是可以的,除了在两种情况下
RecordCount
属性为-1之外。 I would have expected 0 for zero records and 1 for one record. 我期望0表示零记录,1表示一条记录。 I have another section of code that can return a single or multiple rows, and with my testing parameters, it is returning seven rows, and
RecordCount
is correctly showing 7. Does anyone know why I'm getting inconsistent results? 我还有另一段代码可以返回单行或多行,并且使用我的测试参数,它返回了七行,而
RecordCount
正确显示了7。有人知道为什么我得到不一致的结果吗? I need to be able to skip a subsequent piece of code if I get zero results here, and there's no other good way to check, other than trying to access a bad RecordSet object and having a special handler to resume next
if it's a particular error code. 如果我在这里得到零结果,我需要能够跳过随后的代码,并且没有其他好方法可以检查,除了试图访问一个坏的RecordSet对象,并且有一个特殊的处理程序,如果它是一个特殊的错误,可以
resume next
一个操作码。
If your strConnection
is such that you are specifiying a forward-only cursor, then the record count will always be -1
as the result-size cannot be identified with that kind of connection. 如果您的
strConnection
使得您指定了仅向前的游标,那么记录计数将始终为-1
因为无法用这种连接识别结果大小。
From here http://msdn.microsoft.com/de-de/library/windows/desktop/ms676701(v=vs.85).aspx 从这里http://msdn.microsoft.com/de-de/library/windows/desktop/ms676701(v=vs.85).aspx
The cursor type of the Recordset object affects whether the number of records can be determined.
Recordset对象的游标类型影响是否可以确定记录数。 The RecordCount property will return -1 for a forward-only cursor;
对于仅向前的游标,RecordCount属性将返回-1; the actual count for a static or keyset cursor;
静态或键集游标的实际计数; and either -1 or the actual count for a dynamic cursor, depending on the data source.
-1或动态游标的实际计数,具体取决于数据源。
Make sure in your stored procedure GetTemplate
, you have enabled the row count. 确保在存储过程
GetTemplate
,已启用行数。
Example: 例:
CREATE procedure [dbo].[GetTemplate]
AS
BEGIN
SET NOCOUNT OFF -- Enable row count here
...
END
dim FindRecordCount as integer 将FindRecordCount暗淡为整数
If rsTemplate.EOF Then FindRecordCount = 0 Else rsTemplate.MoveLast FindRecordCount = rstRecords.AbsolutePosition End If 如果rsTemplate.EOF然后FindRecordCount = 0否则rsTemplate.MoveLast FindRecordCount = rstRecords.AbsolutePosition结束
Maybe you can try something like this 也许你可以尝试这样的事情
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.