簡體   English   中英

搜索由SQL查詢構建的DataTable

[英]Searching a DataTable built by SQL query

我正在嘗試從存儲用戶信息的SQL Server表中進行SELECT * ,然后將這些條目與Active Directory數據庫進行比較。

如果指定一個特定的人,它會起作用,但是當我嘗試進行全面搜索並將結果放入數據表時,它會出錯。 CodeDescription是相同的。

在數據庫中,代碼就是AD描述字段中的代碼。 因此,什么應該發生的是它拉數據庫中的所有數據放入一個DataTable ,在每個結果DataTable我的搜索廣告和AD的電子郵件字段中返回的電子郵件地址。 同樣,當我輸入特定的名字和姓氏時,它也可以工作,但是當我使用以下代碼時,只會收到未處理的異常錯誤。

這是代碼。 謝謝!

private void btnRun_Click(object sender, EventArgs e)
{
        DataTable dt = new DataTable();

        string APIdbUser = "dbuser";
        string APIdbServer = "dbserver";
        string APIdbUserPW = "dbpassword";
        string APIdbDatabase = "thedatabase";
        string TrustedConnection = "no";
        var ConnectionTimeout = "30";

        using (SqlConnection myConnection = new SqlConnection("user id=" + APIdbUser + ";" +
                                   "password=" + APIdbUserPW + ";server=" + APIdbServer + ";" +
                                   "Trusted_Connection=" + TrustedConnection + ";" +
                                   "database=" + APIdbDatabase + "; " +
                                   "connection timeout=" + ConnectionTimeout))
        {
            using (var myCommand = new SqlCommand("SELECT * FROM dbo.Users WHERE FirstName!='NULL' AND LastName!='NULL' AND Gender!='NULL';", myConnection))
            {
                myConnection.Open();
                using (SqlDataReader dr = myCommand.ExecuteReader())
                {
                    dt.Load(dr);
                }
                myConnection.Close();
            }
        }

        foreach (DataRow dr in dt.Rows)
        {
            string ldapAddress = "LDAP://url";
            string ldapusername = "ldapuser";
            string ldappassword = "ldapuser";

            DirectoryEntry de = new DirectoryEntry(ldapAddress, ldapusername, ldappassword);
            DirectorySearcher ds = new DirectorySearcher(de);

            ds.Filter = "(&((&(objectCategory=person)(objectClass=user)))(givenName=" + dr.Field<string>("FirstName") + ")(sn=" + dr.Field<string>("LastName") + ")(description=" + dr.Field<string>("Code") + "))";
            ds.SearchScope = SearchScope.Subtree;

            SearchResult rs = ds.FindOne();

            if (dr.Field<string>("Code") == rs.GetDirectoryEntry().Properties["description"].Value.ToString())
            {
                MessageBox.Show("This users email address is: " + rs.GetDirectoryEntry().Properties["mail"].Value.ToString());
            }
            //MessageBox.Show(dr.Field<string>("FirstName") + " " + dr.Field<string>("LastName"));
        }
}

這是我固定的方法

if (rs != null)
{
    if (dr.Field<string>("Code") == rs.GetDirectoryEntry().Properties["description"].Value.ToString())
    {
        if (rs.GetDirectoryEntry().Properties["mail"].Value == null)
        {
            MessageBox.Show("This email is N/A.");
        }
        else
        {
            MessageBox.Show("This users email address is: " + rs.GetDirectoryEntry().Properties["mail"].Value.ToString());
        }
    }
}

必須檢查IF NOT null

 if (rs != null)
            {
                if (dr.Field<string>("Code") == rs.GetDirectoryEntry().Properties["description"].Value.ToString())
                {
                    if (rs.GetDirectoryEntry().Properties["mail"].Value == null)
                    {
                        MessageBox.Show("This email is N/A.");
                    }
                    else
                    {
                        MessageBox.Show("This users email address is: " + rs.GetDirectoryEntry().Properties["mail"].Value.ToString());
                    }
                }
            }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM