1

我必须调用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
    ...

但是出现错误(以下由google翻译)

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 
%>

它只显示一个记录结果。

4

2 回答 2

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

于 2012-10-15T08:10:53.103 回答
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
于 2012-10-16T08:35:41.257 回答