【问题标题】:Filling WPF combobox with Active Directory users taking too long用 Active Directory 用户填充 WPF 组合框花费的时间太长
【发布时间】:2012-11-12 17:51:12
【问题描述】:

我有一个绑定到数据表的组合框,我用活动目录中的用户名填充该数据表。此代码大约需要一分钟才能完成。我错过了更好的方法吗?

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

【问题讨论】:

    标签: wpf vb.net performance combobox active-directory


    【解决方案1】:

    不要包含 sn 和 cn 属性,试试下面的过滤器并删除你的 if。

    (&(objectCategory=user)(objectClass=user)(samAccountName=*))
    

    这会查询所有具有现有 samAccount 名称的用户。这应该消除对 if 语句的需要并检查 SN 或 CN。您也可以考虑绑定到比 Datatable 更轻的东西。

    除此之外,您的代码看起来很紧凑。你可能只需要把它放在它自己的线程中。

    【讨论】:

    • 太棒了!这不是答案,但它确实帮助我到达那里。谢谢。
    【解决方案2】:

    @Spevy 用他提出的答案帮助我找到了这个解决方案。

    我将目录条目更改为:

    Dim dirEntry As New System.DirectoryServices.DirectoryEntry("LDAP://myDomain")
    

    我设置了我的搜索过滤器:

    oSearcher.Filter = "(&(objectCategory=user)(objectClass=user))"
    

    并将我的 if 语句更改为:

    If .Properties("samAccountName").Value <> "" AndAlso .Properties("sn").Value <> "" Then
    

    我不确定添加这些代码行的真正作用,但无论它们是否存在,我都会得到相同的结果,所以我删除了它们:

    oSearcher.PropertiesToLoad.Add("samAccountName")
    oSearcher.PropertiesToLoad.Add("givenname")
    oSearcher.PropertiesToLoad.Add("sn")
    oSearcher.PropertiesToLoad.Add("cn")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-01-25
      • 2017-09-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-06
      相关资源
      最近更新 更多