简体   繁体   中英

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. The problem is that sometimes it fines none, which is actually okay, except that the RecordCount property is -1 in both cases. I would have expected 0 for zero records and 1 for one record. 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? 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.

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.

From here 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. The RecordCount property will return -1 for a forward-only cursor; 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.

Make sure in your stored procedure GetTemplate , you have enabled the row count.

Example:

CREATE procedure [dbo].[GetTemplate] 
AS
BEGIN
    SET NOCOUNT OFF -- Enable row count here
    ...

END

dim FindRecordCount as integer

If rsTemplate.EOF Then FindRecordCount = 0 Else rsTemplate.MoveLast FindRecordCount = rstRecords.AbsolutePosition End If

Maybe you can try something like this

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM