C# 从SQL用户ID获取Active Directory名称

C# 从SQL用户ID获取Active Directory名称,c#,sql-server-2005,C#,Sql Server 2005,我有一个包含active directory ID但没有名称的SQL表。所以,问题的核心是我需要找到相关的名字。我曾尝试使用SQL查询active directory,但遇到了问题,因此我的下一次尝试是使用C#.NET并在页面上显示这些ID及其关联的active directory“givenname” 我目前正在尝试使用Gridview来实现这一点 下面的工作表示我试图在ID列旁边创建一个gridview列,并用关联的用户名填充它 现在返回的是第一列中的用户名称,重复了一遍又一遍 DataTa

我有一个包含active directory ID但没有名称的SQL表。所以,问题的核心是我需要找到相关的名字。我曾尝试使用SQL查询active directory,但遇到了问题,因此我的下一次尝试是使用C#.NET并在页面上显示这些ID及其关联的active directory“givenname”

我目前正在尝试使用Gridview来实现这一点

下面的工作表示我试图在ID列旁边创建一个gridview列,并用关联的用户名填充它

现在返回的是第一列中的用户名称,重复了一遍又一遍

DataTable dt = new DataTable();
            using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DB"].ConnectionString))
            {
                SqlDataAdapter adapter = new SqlDataAdapter("spIDsSelect", dbConnection);
                adapter.Fill(dt);
                gvLookup.DataSource = dt;
                gvLookup.DataBind();

            }
            string connection = "LDAP://....";
            using (DirectoryEntry DE = new DirectoryEntry(connection))
            {
                DirectorySearcher dssearch = new DirectorySearcher(connection);
                foreach (DataRow dr in dt.Rows)
                {
                    var name = dt.Rows[0][0].ToString();

                    dssearch.Filter = String.Format("(&(objectCategory=user)(samaccountname={0}))", name);

                    foreach (SearchResult sresult in dssearch.FindAll())
                    {
                        DirectoryEntry de = sresult.GetDirectoryEntry();
                        {
                            foreach (GridViewRow row in gvLookup.Rows)
                            {
                                ((Label)row.FindControl("lbName")).Text = de.Properties["givenname"][0].ToString();
                            }
                        }

                    }
                }
            }



<asp:GridView ID="gvLookup" runat="server" AllowSorting="True"  AutoGenerateColumns="False" 
DataSourceID="Lookup" CellPadding="4" ForeColor="#333333" GridLines="None" HorizontalAlign="Left" 
DataKeyNames="recID" >

<AlternatingRowStyle BackColor="White"/>     
<Columns>

    <asp:BoundField DataField="ID" HeaderText="ID" 
        HeaderStyle-HorizontalAlign="Left">
        <HeaderStyle HorizontalAlign="Left"></HeaderStyle>
    </asp:BoundField>

    <asp:TemplateField HeaderText="Name" >
        <ItemTemplate>
            <asp:Label runat="server" ID="lbName"></asp:Label>
        </ItemTemplate>
    </asp:TemplateField>

</Columns>
</asp:GridView>
DataTable dt=newdatatable();
使用(SqlConnection con=newsqlconnection(ConfigurationManager.ConnectionStrings[“DB”].ConnectionString))
{
SqlDataAdapter=新的SqlDataAdapter(“spIDsSelect”,dbConnection);
适配器填充(dt);
gvLookup.DataSource=dt;
gvLookup.DataBind();
}
字符串连接=“LDAP://…”;
使用(DirectoryEntry DE=newdirectoryEntry(连接))
{
DirectorySearcher dssearch=新的DirectorySearch(连接);
foreach(数据行dr在dt.行中)
{
var name=dt.Rows[0][0].ToString();
dssearch.Filter=String.Format((&(objectCategory=user)(samaccountname={0})),name);
foreach(在dssearch.FindAll()中搜索结果sresult)
{
DirectoryEntry de=sresult.GetDirectoryEntry();
{
foreach(gvLookup.Rows中的GridViewRow行)
{
((Label)row.FindControl(“lbName”)).Text=de.Properties[“givenname”][0].ToString();
}
}
}
}
}

从数据库检索数据后,只需在datatable中添加一列即可:

dt.Columns.Add("ActiveDirectoryName", typeof(string));

这很有效。不需要遍历所有数据行,只需要Gridview。我希望每个ID只有一个结果,并且可以用目录条目名填充Gridview中的标签。重要的是要检查以确保sresult不为null,因为表中的某些用户可能不再有效,这将提示对象引用错误

string connection = "LDAP...";
            using (DirectoryEntry DE = new DirectoryEntry(connection))
            {
                DirectorySearcher dssearch = new DirectorySearcher(connection);
                {
                    var sgID = ((Label)gr.FindControl("lbID")).Text;
                    dssearch.Filter = String.Format("(&(objectCategory=user)(samaccountname={0}))", ID);

                    SearchResult sresult = dssearch.FindOne();

                    if (sresult != null)
                    {
                        DirectoryEntry de = sresult.GetDirectoryEntry();
                        ((Label)gr.FindControl("lbName")).Text = de.Name;
                    }
                }
            }

我已经编辑了你的标题。请参阅“”,其中的共识是“不,他们不应该”。