【问题标题】:directoryservices.directorysearcher loop has error A column named 'cn' already belongs to this datatabledirectoryservices.directorysearcher 循环有错误名为“cn”的列已属于此数据表
【发布时间】:2010-10-04 16:18:26
【问题描述】:

我有一个功能可以在 AD 中搜索不同组的成员。如果我删除循环,我不会收到错误“名为 'cn' 的列已属于此数据表”,但是,我需要遍历每个 OU。

Function getCOMDLNames(ByVal searchStr As String) As DataTable
    Dim MySearchRoot As DirectoryEntry = New DirectoryEntry("path", "usr", "pwd")
    Dim MyDirectorySearcher As New DirectorySearcher(MySearchRoot)
    Dim con As New SqlConnection
    Dim cmdSelect As SqlCommand
    Dim da As New SqlDataAdapter
    Dim cdt As New DataTable
    Dim row As DataRow
    Dim campusGroupArray() As String
    Dim j As Integer = 0

    Try
        con.ConnectionString = ConfigurationManager.AppSettings("sql_dir")
        con.Open()
        cmdSelect = New SqlCommand("...")
        cmdSelect.Connection = con
        da.SelectCommand = cmdSelect
        da.Fill(cdt)
        ReDim campusGroupArray(0 To cdt.Rows.Count)

        'Fill array
        For i As Integer = 0 To cdt.Rows.Count - 1
            row = cdt.Rows(i)
            campusGroupArray(i) = row(0)
        Next

        'Remove empty elements from array
        Dim tempStr As String = String.Join("'", campusGroupArray)
        campusGroupArray = Nothing
        campusGroupArray = tempStr.Split(New Char() {"'"c}, StringSplitOptions.RemoveEmptyEntries)

        Dim MySearchResult As SearchResultCollection

        'Loop through OU's until members are found
        Do
            MyDirectorySearcher.Filter = ("(&(objectCategory=person)(memberof=cn=" & searchStr & ", ou=groups, ou=" & campusGroupArray(j) & ", dc=.., dc=.., dc=.., dc=..))")

            MyDirectorySearcher.SearchScope = SearchScope.Subtree
            MyDirectorySearcher.PropertiesToLoad.Add("cn")

            MyDirectorySearcher.Sort.Direction = System.DirectoryServices.SortDirection.Ascending
            MyDirectorySearcher.Sort.PropertyName = "cn"

            MySearchResult = MyDirectorySearcher.FindAll()
            j += 1
        Loop Until MySearchResult.Count > 0 Or j > campusGroupArray.Length - 1


        Dim myTable As New DataTable
        Dim colName As String

        'Create columns in table
        For Each colName In MyDirectorySearcher.PropertiesToLoad
            myTable.Columns.Add(colName, GetType(System.String))
        Next

        Dim result As SearchResult

        'Fill table
        For Each result In MySearchResult
            Dim dr As DataRow = myTable.NewRow()
            For Each colName In MyDirectorySearcher.PropertiesToLoad
                If result.Properties.Contains(colName) Then
                    dr(colName) = CStr(result.Properties(colName)(0))
                Else
                    dr(colName) = ""
                End If
            Next
            myTable.Rows.Add(dr)
        Next
        Return myTable
    Catch ex As Exception
        Return Nothing
    End Try
End Function

【问题讨论】:

    标签: vb.net active-directory directoryservices


    【解决方案1】:

    您可以循环访问任意数量的 OU - 但您需要定义要加载的属性列表(.PropertiesToLoad 属性)在循环开始之前一次(这确实适用于所有在对目录搜索器的调用之间不会更改的属性 - 例如搜索范围、排序方向等 - 为每次迭代设置这些属性绝对没有意义 - 设置一次并完成。

    ' define the list of properties to load for each search result ONCE before the loop
    MyDirectorySearcher.PropertiesToLoad.Add("cn")
    MyDirectorySearcher.SearchScope = SearchScope.Subtree
    MyDirectorySearcher.Sort.Direction = SortDirection.Ascending
    MyDirectorySearcher.Sort.PropertyName = "cn"
    
     ' *THEN* Loop through OU's until members are found
    Do
        MyDirectorySearcher.Filter = ("(&(objectCategory=person)(memberof=cn=" & searchStr & ", ou=groups, ou=" & campusGroupArray(j) & ", dc=.., dc=.., dc=.., dc=..))")
    
         MySearchResult = MyDirectorySearcher.FindAll()
         j += 1
    Loop Until MySearchResult.Count > 0 Or j > campusGroupArray.Length - 1
    

    PropertiesToLoad 仅定义搜索结果将包含的属性列表 - 这是每次搜索的相同列表,并且只能包含一次属性名称(例如 cn)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多