2012-10-15 32 views
1

我必須撥打recordCount函數才能獲得記錄集的計數。 但是,一旦我調用recordCount函數,記錄集就失去控制。發生BOF或EOF錯誤後,請致電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 
%> 

這說明只有一個記錄的結果。

回答

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 
+0

什麼是智能解決方案!我嘗試了你的建議,但它不起作用。 'moveFirst'似乎不適用於我的'IIS 6.0'。我認爲我的服務器有一些配置問題或sometihng我找不到。 – Deckard

+0

你的意思是「不起作用」?當你有'MoveFirst'行時會發生什麼? –

+0

它沒有顯示任何結果。 – Deckard

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

+0

這是更好,但只顯示一個記錄的結果。並且oricinal代碼是'做直到'。我犯了一個錯誤。 – Deckard

+0

@Deckard好的,我沒有注意到,循環測試做的同時adoRecordset.EOF ...應該是做,而不是** adoRecordset.EOF(或做直到..)順便說一句,你是否仍然需要你totalcnt?你可以用adoRecordset替換totalcnt.EOF – AardVark71

+0

它仍然只顯示一個結果,但我加了'not'。 – Deckard

相關問題