我目前正在使用givenName *和sn *搜索AD。以下代码仅在结果较小时有效。当结果相当大时,它不起作用。我不明白为什么返回空引用。
DataTable userTable = new DataTable();
try
{
searchUser.Filter = "(&(objectClass=user)(&(givenName=" + FirstName.Text.Trim() + "*)(sn=" + LastName.Text.Trim() + "*)))";
// searchUser.PropertiesToLoad.AddRange(new String[] { "displayName", "extensionAttribute13", "description" });
searchUser.PropertiesToLoad.Add("displayname");
searchUser.PropertiesToLoad.Add("extensionAttribute13");
searchUser.PropertiesToLoad.Add("description");
SearchResultCollection multipleResults = searchUser.FindAll();
userTable.Columns.Add("Name",System.Type.GetType("System.String"));
userTable.Columns.Add("Gentiva ID", System.Type.GetType("System.String"));
userTable.Columns.Add("Location", System.Type.GetType("System.String"));
foreach (SearchResult result in multipleResults)
{
DataRow dr = userTable.NewRow();
DirectoryEntry de = result.GetDirectoryEntry();
if (result.GetDirectoryEntry().Properties != null)
{
if(result.GetDirectoryEntry().Properties["displayname"].Value==null)
{
dr["Name"] = " ";
// MessageBox.Show("Null Hit - display");
}
else if (result.GetDirectoryEntry().Properties["displayname"].Value != null)
{
dr["Name"] = (string)de.Properties["displayname"].Value.ToString();
}
if (result.GetDirectoryEntry().Properties["extensionAttribute"].Value == null)
{
dr["Gentiva ID"] = " ";
// MessageBox.Show("Null Hit - extensions");
}
else if (result.GetDirectoryEntry().Properties["extensionAttribute"].Value != null)
{
dr["Gentiva ID"] = (string)de.Properties["extensionAttribute"].Value.ToString();
}
if (result.GetDirectoryEntry().Properties["description"].Value == null)
{
dr["Location"] = " ";
// MessageBox.Show("Null Hit - description");
}
else if (result.GetDirectoryEntry().Properties["description"].Value != null)
{
dr["Location"] = (string)de.Properties["displayname"].Value.ToString();
}
userTable.Rows.Add(dr);
de.Close();
de.Dispose();
}
else
{
// MessageBox.Show("Null Hit");
}
}
grdviewMultiple.ItemsSource = userTable.AsDataView();
grdviewMultiple.Visibility = Visibility.Visible;
}
catch(Exception ex)
{
throw ex;
}
答案 0 :(得分:1)
尝试以下操作只需将其粘贴到您拥有的内容中并确保调试它。 在我看来,大数据实际上与您的问题无关我认为您没有进行适当的检查和转换类型而且您收到错误
try
{
searchUser.Filter = "(&(objectClass=user)(&(givenName=" + FirstName.Text.Trim() + "*)(sn=" + LastName.Text.Trim() + "*)))";
searchUser.PropertiesToLoad.AddRange(new String[] { "displayName", "extensionAttribute13", "description" });
SearchResultCollection multipleResults = searchUser.FindAll();
DataTable userTable = new DataTable();
userTable.Columns.Add("Name", System.Type.GetType("System.String"));
userTable.Columns.Add("Gentiva ID", System.Type.GetType("System.Int32"));
userTable.Columns.Add("Location", System.Type.GetType("System.String"));
foreach (SearchResult result in multipleResults)
{
DataRow dr = userTable.NewRow();
DirectoryEntry de = result.GetDirectoryEntry();
if (result.GetDirectoryEntry().Properties != null)
{
if(result.GetDirectoryEntry().Properties["displayname"].Value==null)
{
dr["Name"] = "";
}
else
{
dr["Name"] = (string)de.Properties["displayname"].Value.ToString();
}
if (result.GetDirectoryEntry().Properties["extensionAttribute"].Value == null)
{
dr["Gentiva ID"] = DBNull.Value;
}
else
{
dr["Gentiva ID"] = (int)de.Properties["extensionAttribute13"].Value;
}
if (result.GetDirectoryEntry().Properties["description"].Value == null)
{
dr["Location"] = "";
}
else
{
dr["Location"] = (string)de.Properties["description"].Value.ToString();
}
}
userTable.Rows.Add(dr);
}
de.Close();
de.Dispose();
grdviewMultiple.ItemsSource = userTable.AsDataView();
grdviewMultiple.Visibility = Visibility.Visible;
}
catch(ActiveDirectoryOperationException adEx)
{
MessageBox.Show(adEx.ToString());
de.Close();
de.Dispose()
}