Hi Guys,

I am trying to retrieve all the users in the Active Directory and dump into a Sql Server Table(Sql2008). I was able to search one employee at a time but could not able to search all the employees in the Active Directory. Basically i want to retrieve 1.SamAccountname and 2.Employeenumber. Can you guys help me with the code?
I am using VS2010/VB.NET/ASP.NET. Appreciate your help.
Thanks

From within SQL, you can use either the system stored procedure sp_addlinkedserver or the SQL Management Studio to set up a linked server to Active Directory.

sp_addlinkedserver 'ADSI', 'Active Directory Service Interfaces', 'ADSDSOObject', 'adsdatasource'

The next step is to figure out what the scope and elements that you wish to include in your query. For example, you may want to query user objects in the Mkt OU.

LDAP://ou=ou=Mkt,dc=domain,dc=local where objectclass = user

create the SQL query.

SELECT * FROM OpenQuery(ADSI, 'SELECT displayName, mail FROM ''LDAP:// ou=Mkt,dc=domain,DC=local''where objectclass = ''user'' ')

Hi,

Thanks for the info. I already tried the one you suggested, but i am getting an error when i run the query against Management Studio. any idea?
An error occurred while preparing the query "SELECT samaccountname,mail FROM "LDAP://............"where objectcategory = "user" " for execution against OLE DB provider "ADSDSOObject" for linked server "ADSI".

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.