0

我不确定我在这里做错了什么。

我正在尝试从 Active Directory 中检索一些值并将它们插入到我们的 sql server 数据库中。

我在第 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
4

1 回答 1

0

您与 MS SQL Server 的连接字符串 - "driver=Sql Server; ..."- 看起来很可疑。使用连接字符串的源,我希望得到类似的东西"Provider= ...",但如果没有关于版本和访问策略(odbc、oledb、...)的更多信息,很难说。

我在这里写过关于使用 .udl 文件在 GUI 中试验连接字符串的文章。

于 2012-07-02T18:40:20.030 回答