调用recordCount后出现BOF或EOF错误

时间:2012-10-15 08:02:57

标签: asp-classic vbscript

我必须调用recordCount函数来获取记录集的计数。 但是一旦我调用recordCount函数,记录集就会失控。

...
Dim objRootDSE, strDNSDomain, adoCommand, adoConnection
Set adoCommand = CreateObject("ADODB.Command")
'Set adoRecordset = adoCommand.Execute
Set adoRecordset = Server.CreateObject ("ADODB.Recordset") 
adoRecordset.cursorType = 3
adoRecordset.CursorLocation = adUseClient 
adoRecordset = adoCommand.Execute
...


totalcnt = adoRecordset.recordCount

If totalcnt > 0 Then 
...
    Do until adoRecordset.EOF
        ' Retrieve values... But it fails because it seems adoRecordset is in EOF
        ...

所以我使用movefirst并尝试检索值。

If adoRecordset.recordCount > 0 Then 
                                            adoRecordset.movefirst
    ...

但它发生错误(下面由谷歌翻译)

ADODB.Recordset 오류 '800a0bcd' 
BOF or EOF is True, or the current record has been deleted. Requested operation requires a current record.

如果我没有打电话给recordCount,那就没问题了。但我应该知道记录的数量。

整个代码是:

    <%
'On Error Resume next
 Dim objRootDSE, strDNSDomain, adoCommand, adoConnection
 Dim strBase, strFilter, strAttributes, strQuery, adoRecordset
 Dim strDN, strUser, strPassword, objNS, strServer
 Dim name,company,physicalDeliveryOfficeName

 Const ADS_SECURE_AUTHENTICATION = 1
 Const ADS_SERVER_BIND = 0

 ' Specify a server (Domain Controller).
 strServer = "my_ad_server_domain"

 ' Specify or prompt for credentials.
 strUser = "my_account"
 strPassword = "my_passwrd"

 ' Determine DNS domain name. Use server binding and alternate
 ' credentials. The value of strDNSDomain can also be hard coded.
 Set objNS = GetObject("LDAP:")
 Set objRootDSE = objNS.OpenDSObject("LDAP://" & strServer & "/RootDSE", _
      strUser, strPassword, _
      ADS_SERVER_BIND Or ADS_SECURE_AUTHENTICATION)
 strDNSDomain = objRootDSE.Get("defaultNamingContext")

 ' Use ADO to search Active Directory.
 ' Use alternate credentials.
 Set adoCommand = CreateObject("ADODB.Command")
 Set adoConnection = CreateObject("ADODB.Connection")
 adoConnection.Provider = "ADsDSOObject"
 adoConnection.Properties("User ID") = strUser
 adoConnection.Properties("Password") = strPassword
 adoConnection.Properties("Encrypt Password") = True
 adoConnection.Properties("ADSI Flag") = ADS_SERVER_BIND _
      Or ADS_SECURE_AUTHENTICATION
 adoConnection.Open "Active Directory Provider"
 Set adoCommand.ActiveConnection = adoConnection

 ' Search entire domain. Use server binding.
 strBase = "<LDAP://" & strServer & "/" & strDNSDomain & ">"

 ' Search for all users.
 strFilter = "(&(objectCategory=user)(ExADObjectStatus=10)(samaccountname=*"&"my_search_value"&"*))"

 ' Comma delimited list of attribute values to retrieve.
 strAttributes = "name,company,physicalDeliveryOfficeName"

 ' Construct the LDAP query.
 strQuery = strBase & ";" & strFilter & ";" _
      & strAttributes & ";subtree"

 ' Run the query.
 adoCommand.CommandText = strQuery
 adoCommand.Properties("Page Size") = 100
 adoCommand.Properties("Timeout") = 60
 adoCommand.Properties("Cache Results") = False
 Set adoRecordset = adoCommand.Execute




if not adoRecordset.EOF then 
    totalcnt = adoRecordset.recordCount 
    If totalcnt > 0 Then  
    Response.write 111
        Do until adoRecordset.EOF
            name = adoRecordset.Fields("name").Value
              company = adoRecordset.Fields("company").Value
              physicalDeliveryOfficeName = adoRecordset.Fields("physicalDeliveryOfficeName").Value
              Response.Write name & "<br/>"
              Response.Write company & "<br/>"
              Response.Write physicalDeliveryOfficeName
              adoRecordset.MoveNext
        Loop 
    end if 
end if 



 ' Clean up.
 adoRecordset.Close
 adoConnection.Close 
%>

只显示一个记录结果。

2 个答案:

答案 0 :(得分:1)

如错误所示,如果记录集中没有记录,则recordCount将失败。

您可以在代码块之前对此进行测试。试试这个:

if not adoRecordset.EOF then
    totalcnt = adoRecordset.recordCount
    If totalcnt > 0 Then 
        ...
        Do while not adoRecordset.EOF
        ...
        Loop
    end if
end if

修改:更正了测试 adoRecordset.eof

的循环

答案 1 :(得分:1)

您可以尝试从不同角度面对问题。而不是尝试修复内部recordCount属性(您不能),只需自己计算记录:

totalcnt = 0
Do until adoRecordset.EOF
    totalcnt = totalcnt + 1
    adoRecordset.MoveNext
Loop

If totalcnt>0 Then
    adoRecordset.MoveFirst
    Do until adoRecordset.EOF
        name = adoRecordset.Fields("name").Value
        '...
        adoRecordset.MoveNext
    Loop
End If

更新:在特定情况下,MoveFirst只是失败,可能是因为它是LDAP而不是来自数据库的普通查询。要彻底破坏这个,你可以在迭代记录时填充你自己的集合,然后尽可能多地使用该集合:

Dim oData, oField, tempArray
Set oData = Server.CreateObject("Scripting.Dictionary")
totalcnt = 0
For Each oField In adoRecordset.Fields
    oData.Add oField.Name, Array()
Next
Do until adoRecordset.EOF
    For Each oField In adoRecordset.Fields
        tempArray = oData(oField.Name)
        ReDim Preserve tempArray(UBound(tempArray) + 1)
        tempArray(UBound(tempArray)) = oField.Value
        oData(oField.Name) = tempArray
    Next
    totalcnt = totalcnt + 1
    adoRecordset.MoveNext
Loop
adoRecordset.Close

Dim x
If totalcnt>0 Then
    Response.Write("found total of " & totalcnt & " records<br />")
    For x=0 To totalcnt-1
        name = oData("name")(x)
        company = oData("company")(x)
        physicalDeliveryOfficeName = oData("physicalDeliveryOfficeName")(x)
        Response.Write name & "<br/>"
        Response.Write company & "<br/>"
        Response.Write physicalDeliveryOfficeName
    Next
End If