Asp classic 呼叫记录计数后出现BOF或EOF错误

Asp classic 呼叫记录计数后出现BOF或EOF错误,asp-classic,vbscript,Asp Classic,Vbscript,我必须调用recordCount函数来获取记录集的计数。 但是一旦我调用recordCount函数,记录集就失去了控制 ... Dim objRootDSE, strDNSDomain, adoCommand, adoConnection Set adoCommand = CreateObject("ADODB.Command") 'Set adoRecordset = adoCommand.Execute Set adoRecordset = Server.CreateObject ("ADO

我必须调用
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
    ...
但是它发生了一个错误(下面是谷歌翻译的)

如果我没有调用
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 
%>
0那么
回复111
直到adoRecordset.EOF为止
name=adoRecordset.Fields(“name”).Value
公司=adoRecordset.Fields(“公司”).Value
physicalDeliveryOfficeName=adoRecordset.Fields(“physicalDeliveryOfficeName”).Value
响应。写入名称&“
” 回答。写公司&“
” 响应。写入physicalDeliveryOfficeName adoRecordset.MoveNext 环 如果结束 如果结束 “清理。 adoRecordset,关闭 连接,关闭 %>

它只显示记录的一个结果。

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

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

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

编辑:更正循环以测试adoRecordset。eof

您可以尝试从不同的角度面对问题。与其尝试修复内部
recordCount
属性(您无法修复),不如自己对记录进行计数:

totalcnt=0
直到adoRecordset.EOF为止
总碳纳米管=总碳纳米管+1
adoRecordset.MoveNext
环
如果totalcnt>0,则
adoRecordset.MoveFirst
直到adoRecordset.EOF为止
name=adoRecordset.Fields(“name”).Value
'...
adoRecordset.MoveNext
环
如果结束
更新:在这种特定情况下,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
Dim oData、oField、temparay
设置oData=Server.CreateObject(“Scripting.Dictionary”)
总碳纳米管=0
对于adoRecordset.Fields中的每个字段
oData.addofield.Name,数组()
下一个
直到adoRecordset.EOF为止
对于adoRecordset.Fields中的每个字段
tempArray=oData(oField.Name)
重拨保留临时数组(UBound(临时数组)+1)
tempArray(UBound(tempArray))=字段值
oData(oField.Name)=临时数组
下一个
总碳纳米管=总碳纳米管+1
adoRecordset.MoveNext
环
adoRecordset,关闭
暗x
如果totalcnt>0,则
Response.Write(“找到的“&totalcnt&”记录总数
”) 对于x=0到总CNT-1 名称=oData(“名称”)(x) 公司=oData(“公司”)(x) physicalDeliveryOfficeName=oData(“physicalDeliveryOfficeName”)(x) 响应。写入名称&“
” 回答。写公司&“
” 响应。写入physicalDeliveryOfficeName 下一个 如果结束
更好,但它只显示记录集的一个结果。最初的代码是
do,直到
。我犯了一个错误。@Deckard好的,我没有注意到循环在do上测试,而adoRecordset.EOF。。。这应该是Do while not adoRecordset.EOF(或Do until…)顺便问一下,您还需要您的totalcnt吗?您可以用adoRecordset上的测试替换totalcnt。EOFY您的记录数大于该记录数吗?在你的循环中有移动下一个吗?是的,我有。我将把全部代码添加到我的问题中。非常感谢。我刚加了一句。代码看起来还可以。。我会跳过对recordCount的检查(而改用对adoRecordset.oef的检查)。不知道为什么你只看到一排。。也许ldap查询已经将您定位到了最后一条记录,在Do while循环之前添加adoRecordset.movefirst是有意义的,这是一个多么聪明的解决方案!我试过你的建议,但没用
moveFirst
在我的
IIS 6.0
中似乎不起作用。我想我的服务器有一些配置问题或者我找不到的东西。你说的“不工作”是什么意思?当您有
MoveFirst
行时会发生什么?当您有
Response.Write(“count:&totalcnt&“
”)时会发生什么?它显示正常结果
count:13
。我在adoRecordset.EOF
之前将该代码添加到
Do的循环中,但它没有出来。@Deckard-wird。看我的编辑,这将99%的工作,虽然它可能有点过分。
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