Hi all, I'm in an environment of VB.NET, ASP.NET and Novell eDirectory 8.5*. What I need to do is to retrieve all customers (about 10000+) with their firstname and lastname. I tried to use System.DirectorySearcher, but its performance of retrieving and looping
thru such big amount of data to display on .aspx page is very very slow, especially looping thru the SearchResultsCollection took a lot of time. Back in VB6 world, I had some working code that used ADO (Command & RecordSet, etc) to query the same LDAP server.
Then I wonder if ADO.NET can work the same way, that's where the following test code came from. Also I wonder if ADO.NET, particularly OleDbDataReader (or maybe OleDbDataAdapter), can speed things up. If yes, the result of DataSet will make things such as
binding to a dropdown much easier. The following is my test code, but it errors out in the bold section. (maybe I used dr in a wrong way?) Does anybody know how to make it work? Anybody can provide a good working example? (I have been googling for it , but
can't find one) In a word, any of your input will be great!! Thank you!! '========================================= Dim oConnection As OleDbConnection Dim oCmd As OleDbCommand Dim dt As DataTable = New DataTable() Dim strADOQuery As String Dim strFilter As
String oConnection = New OleDbConnection() oCmd = New OleDbCommand() oConnection.ConnectionString = "Provider=ADsDSOObject;" oConnection.Open() oCmd.Connection = oConnection '// Build the query string strFilter = "(objectclass=user)" '// Execute the query
strADOQuery = ";" + strFilter + ";" + "givenname,sn" + ";subtree" oCmd.CommandText = strADOQuery oCmd.CommandTimeout = 600
Dim dr As OleDbDataReader dr = oCmd.ExecuteReader While dr.Read Dim strTemp As String strTemp = dr.GetValue(0) End While
You are doing lots of individual LDAP requests. Better to do one request higher up the tree and loop through all the names it throws back. I also find it is very fast if you stick to the pure .Net Method of using System.DirectorySearcher Like this:
Private Function GetAllUsers() As DataTable
' *** get list of active directory users ***
' setting up the lookup to AD
Dim adEntry As New DirectoryEntry("LDAP://myserver.mydomain.com/DC=
mydomain,DC=com")
' define which fields to retrieve from AD
Dim adSearcher As New DirectorySearcher(adEntry)
adSearcher.Filter = "(&(objectCategory=person)(objectClass=user))"
adSearcher.PropertiesToLoad.Add("cn")
adSearcher.PropertiesToLoad.Add("sAMAccountName")
adSearcher.PropertiesToLoad.Add("mail")
' define a datatable and add the results to it
Dim adResults As SearchResultCollection
Dim dt As New DataTable("AD_Users")
dt.Columns.Add(New DataColumn("AccName", GetType(System.String)))
dt.Columns.Add(New DataColumn("Name", GetType(System.String)))
dt.Columns.Add(New DataColumn("Email", GetType(System.String)))
Dim dr As DataRow
adResults = adSearcher.FindAll
For Each adResult As SearchResult In adResults
' add the results to the datatable
dr = dt.NewRow()
dr(0) = adResult.Properties("sAMAccountName")(0).ToString()
dr(1) = adResult.Properties("cn")(0).ToString()
dr(2) = adResult.Properties("mail")(0).ToString()
dt.Rows.Add(dr)
Next
Return dt
End Function
I previously shared a function called FindUsers() that will retrieve a list of users from a domain and return a DataSet. It uses caching to speed subsequent requests. Read about it here: 163706
aspworker
Member
10 Points
2 Posts
How to query LDAP server by using ADO.NET
Aug 06, 2003 08:42 PM|LINK
rohancragg
Member
138 Points
31 Posts
Re: How to query LDAP server by using ADO.NET
Aug 07, 2003 01:31 PM|LINK
Private Function GetAllUsers() As DataTable ' *** get list of active directory users *** ' setting up the lookup to AD Dim adEntry As New DirectoryEntry("LDAP://myserver.mydomain.com/DC= mydomain,DC=com") ' define which fields to retrieve from AD Dim adSearcher As New DirectorySearcher(adEntry) adSearcher.Filter = "(&(objectCategory=person)(objectClass=user))" adSearcher.PropertiesToLoad.Add("cn") adSearcher.PropertiesToLoad.Add("sAMAccountName") adSearcher.PropertiesToLoad.Add("mail") ' define a datatable and add the results to it Dim adResults As SearchResultCollection Dim dt As New DataTable("AD_Users") dt.Columns.Add(New DataColumn("AccName", GetType(System.String))) dt.Columns.Add(New DataColumn("Name", GetType(System.String))) dt.Columns.Add(New DataColumn("Email", GetType(System.String))) Dim dr As DataRow adResults = adSearcher.FindAll For Each adResult As SearchResult In adResults ' add the results to the datatable dr = dt.NewRow() dr(0) = adResult.Properties("sAMAccountName")(0).ToString() dr(1) = adResult.Properties("cn")(0).ToString() dr(2) = adResult.Properties("mail")(0).ToString() dt.Rows.Add(dr) Next Return dt End Functiondunnry
Star
9098 Points
1806 Posts
Re: How to query LDAP server by using ADO.NET
Aug 07, 2003 03:46 PM|LINK
Weblog
The Book
LDAP Programming Help
aspworker
Member
10 Points
2 Posts
Thanks a lot!
Aug 08, 2003 04:21 PM|LINK