我有一个绑定到数据表的组合框,我用活动目录中的用户名填充该数据表。此代码大约需要一分钟才能完成。我错过了更好的方法吗?
Function users() As DataTable
Dim dt As DataTable
Dim dr As DataRow
Dim idCoulumn As DataColumn
Dim nameCoulumn As DataColumn
Dim dirEntry As New System.DirectoryServices.DirectoryEntry("LDAP://CN=Users,DC=myDomain,DC=local")
Dim oSearcher As DirectorySearcher = New DirectorySearcher(dirEntry)
Dim oResults As SearchResultCollection
oSearcher.PropertiesToLoad.Add("samAccountName")
oSearcher.PropertiesToLoad.Add("givenname")
oSearcher.PropertiesToLoad.Add("sn")
oSearcher.PropertiesToLoad.Add("cn")
oSearcher.Filter = "objectCategory=person"
oResults = oSearcher.FindAll
dt = New DataTable()
idCoulumn = New DataColumn("ID", Type.GetType("System.String"))
nameCoulumn = New DataColumn("Name", Type.GetType("System.String"))
dt.Columns.Add(idCoulumn)
dt.Columns.Add(nameCoulumn)
For Each oResult In oResults
With oResult.GetDirectoryEntry()
If .Properties("cn").Value <> "" AndAlso .Properties("samAccountName").Value <> "" AndAlso .Properties("sn").Value <> "" Then
dr = dt.NewRow()
dr("ID") = .Properties("samAccountName").Value
dr("Name") = String.Format("{0},{1} : {2}", .Properties("sn").Value, .Properties("givenname").Value, .Properties("samAccountName").Value)
dt.Rows.Add(dr)
End If
End With
Next
dt.DefaultView.Sort = "Name"
Return dt
End Function