2017-02-24 88 views
0

我試圖做從那裏用戶信息存儲在SQL Server表一個SELECT *,然後將這些條目比較的Active Directory數據庫建立一個DataTable。搜索由SQL查詢

如果我指定一個特定的人它的工作原理,但它的錯誤,當我嘗試地毯式搜索,並把結果放到一個數據表。 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()); 
     } 
    } 
} 
+0

'WHERE名字!= 'NULL' 和姓氏!= 'NULL' 和性別! ='NULL'' - 爲什麼你在表中存儲文本''NULL'而不是將字段'NULL'存儲? – Siyual

+0

你正在得到什麼錯誤?在哪行代碼? –

+0

「NullReferenceUnhandled」類型的未處理的異常「System.NullReferenceException」發生在項目名。 @Siyual我實際上寧願讓它成爲SELECT * FROM dbo.users並將其留在那裏。這真的沒關係,我只是得到一個錯誤。 IF語句發生錯誤。 –

回答

0

不得不檢查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()); 
        } 
       } 
      }