2012-07-02 30 views
0

我不確定我在這裏做錯了什麼。我正在從Active Directory中檢索信息並嘗試插入數據庫

我想從Active Directory中檢索一些值並將它們插入到我們的sql服務器數據庫中。

我一直在23行收到錯誤 「提供者:未指定錯誤」

這是行23 - >設置adoRecordset = adoCommand.Execute

下面是我使用的代碼和感謝:

'============== 
Set adoCommand = CreateObject("ADODB.Command") 
Set adoConnection = CreateObject("ADODB.Connection") 
adoConnection.Provider = "ADsDSOObject" 
adoConnection.Open "Active Directory Provider" 
adoCommand.ActiveConnection = adoConnection 
' Search entire Active Directory domain. 
Set objRootDSE = GetObject("LDAP://RootDSE") 
strDNSDomain = objRootDSE.Get("defaultNamingContext") 
strBase = "<LDAP://" & strDNSDomain & ">" 
strFilter = "(&(objectCategory=person)(objectClass=user))" 
'strFilter = "(&(objectClass=computer)(cn=" & strComputer & "))" 
' Comma delimited list of attribute values to retrieve. 
'strAttributes = "sAMAccountName,cn" 
strAttributes = "employeeID,name,givenName,sname" 
' Construct the LDAP syntax query. 
strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree" 
adoCommand.CommandText = strQuery 
adoCommand.Properties("Page Size") = 100 
adoCommand.Properties("Timeout") = 30 
adoCommand.Properties("Cache Results") = False 
' Run the query. 
Set adoRecordset = adoCommand.Execute **<--Error pointing to this line** 
' ******** SET UP DB CONNECTION ********** 
Set dbConn = createobject("ADODB.Connection") 
dbConn.ConnectionString = "driver=Sql Server;server=serverName;uid=myuser;pwd=mypwd;database=myDB" 
dbConn.Open 
' ***************************************** 
strDetails = "" 
' Enumerate the resulting recordset. 
Do Until adoRecordset.EOF 
    ' Retrieve values and display. 
     strEmpID = adoRecordset.Fields("employeeID").Value 
     strName = adoRecordset.Fields("name").Value 
     strfname = adoRecordset.Fields("givenName").Value 
     strlname = adoRecordset.Fields("sname").Value 
' ******* INSERT INTO DB ************* 
     dbConn.Execute "INSERT INTO myTable Values('" & strEmpID & "','" & strName & "','" & strfname & "','" & strlname & "')" 
' ************************************ 
     ' Move to the next record in the recordset. 
    adoRecordset.MoveNext 
Loop 
' Clean up. 
adoRecordset.Close 
Set adoRecordset = Nothing 
adoConnection.Close 

回答

0

您的連接字符串到MS SQL服務器 - "driver=Sql Server; ..." - 看起來很腥。使用the source for connection strings,我期望得到類似"Provider= ..."的東西,但沒有關於版本和訪問策略(odbc,oledb,...)的進一步信息,很難說清楚。

我已經寫了關於使用.udl文件在GUI中嘗試連接字符串here

+0

嗨艾赫哈, 感謝您的好鏈接。我創建了udl並在我的代碼上使用了鏈接,但仍然出現相同的錯誤。 看着這個錯誤,連接字符串在第26行,但是錯誤在第23行。我懷疑錯誤是在錯誤行上面尋找東西。您能否讓我知道您可能需要什麼具體信息? 我想使用代碼指向我們的Active Directory,檢索一些數據並將數據插入數據庫。 – Kenny

相關問題