0
我试图做从那里用户信息存储在SQL Server表一个SELECT *
,然后将这些条目比较的Active Directory数据库建立一个DataTable。搜索由SQL查询
如果我指定一个特定的人它的工作原理,但它的错误,当我尝试地毯式搜索,并把结果放到一个数据表。 Code
和Description
是一个相同的。
在数据库中,该代码是什么是在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());
}
}
}
'WHERE名字!= 'NULL' 和姓氏!= 'NULL' 和性别! ='NULL'' - 为什么你在表中存储文本''NULL'而不是将字段'NULL'存储? – Siyual
你正在得到什么错误?在哪行代码? –
“NullReferenceUnhandled”类型的未处理的异常“System.NullReferenceException”发生在项目名。 @Siyual我实际上宁愿让它成为SELECT * FROM dbo.users并将其留在那里。这真的没关系,我只是得到一个错误。 IF语句发生错误。 –