简体   繁体   English

无法将 DataGridView 与 Recordset 绑定

[英]Not able to bind DataGridView with Recordset

I am using below code to query Active Directory and get list of users.我正在使用以下代码查询 Active Directory 并获取用户列表。 The code is working in VB Macro.该代码在 VB 宏中运行。 I modified a bit syntactically to make it work on VB.NET (VS 2022).我在语法上做了一些修改,使其适用于 VB.NET (VS 2022)。

The RecordCount is appearing as 900 but I am not able to bind it to DataGridView. RecordCount 显示为 900,但我无法将它绑定到 DataGridView。 Not enough knowledge of VB.对VB的了解不够。 I referred to samples which use OleDB DataAdapter but in the code below the command object is different so it throws error.我提到了使用 OleDB DataAdapter 的示例,但在下面的代码中,命令 object 不同,因此它会抛出错误。

Also, this line throws error.此外,此行会引发错误。 It is working in Excel Macro.它在 Excel 宏中工作。

oCommand1.Properties("SearchScope") = 2

Please advise how to display records in Grid:请告知如何在网格中显示记录:

'Open the connection.
'This is the ADSI OLE-DB provider name
oConnection1.Provider = "ADsDSOObject"
oConnection1.Open("Active Directory Provider")

oCommand1.ActiveConnection = oConnection1

strQuery = "select c, l, SAMAccountName,displayName, distinguishedName, cn, sn,givenName,title,mail, department, manager, userAccountControl " &
" from 'GC://dc=TestAD,dc=net'" &
"WHERE objectCategory='Person'" &
"AND objectClass='user'"

oCommand1.CommandText = strQuery
oCommand1.Properties("SearchScope") = 2

rs = oCommand1.Execute()
lblRecords.Text = rs.RecordCount
DataGridView1.DataSource = rs

oConnection1.Close()

Use Directory services and LDAP query:使用目录服务和 LDAP 查询:

Imports System.DirectoryServices

{......}
    
    Dim oD As DirectoryEntry
    Dim oS As DirectorySearcher
                
    oD = New DirectoryEntry("LDAP://RootDSE")
    oS = New DirectorySearcher(oD)
            
    oS.Filter = "(&(objectClass=user))"
    oS.SearchScope = SearchScope.Subtree
    
    ''''''''''''''''''''''
    'by default all objects property will be retrieve
    'if you just need somme of them use
    'oS.PropertiesToLoad.Add("SAMAccountName")
    'oS.PropertiesToLoad.Add("displayName")
    'oS.PropertiesToLoad.Add("CN")
    'etc...
    'that will speed up search
    ''''''''''''''''''''''
    
    Dim src As SearchResultCollection = oS.FindAll()

    oD.Close()
    oS.Dispose()

    Dim dt As New DataTable()
    dt.Columns.Add("samaccountname")
    dt.Columns.Add("givenName")
    dt.Columns.Add("sn")
    dt.Columns.Add("mail")
    ' add here others columns needed in dt
    
    For Each sr As SearchResult In src
        Dim dr As DataRow = dt.NewRow()
        Dim de As DirectoryEntry = sr.GetDirectoryEntry()
        dr("samaccountname") = de.Properties("samaccountname").Value.ToString()
        dr("givenName") = de.Properties("givenName").Value.ToString()
        dr("sn") = de.Properties("sn").Value.ToString()
        dr("mail") = de.Properties("mail").Value.ToString()
        'etc... fill others dt columns based on propertys needed
        dt.Rows.Add(dr)
        de.Close()
    Next sr

    lblRecords.Text = dt.rows.count-1
    DataGridView1.DataSource = dt

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM