C# 搜索由SQL查询生成的数据表
我试图从存储用户信息的SQL Server表中执行C# 搜索由SQL查询生成的数据表,c#,sql-server,datatable,active-directory,C#,Sql Server,Datatable,Active Directory,我试图从存储用户信息的SQL Server表中执行选择*,然后将这些条目与Active Directory数据库进行比较 如果我指定了一个特定的人,它是有效的,但是当我尝试覆盖搜索并将结果放入数据表时,它会出错代码和说明是同一个目录 在数据库中,代码就是AD的description字段中的代码。因此,应该发生的是,它将数据库中的所有数据拉入一个DataTable,对于DataTable中的每个结果,我搜索AD并在AD的email字段中返回电子邮件地址。同样,当我输入特定的名字和姓氏时,它会起作用
选择*
,然后将这些条目与Active Directory数据库进行比较
如果我指定了一个特定的人,它是有效的,但是当我尝试覆盖搜索并将结果放入数据表时,它会出错<代码>代码和说明
是同一个目录
在数据库中,代码就是AD的description字段中的代码。因此,应该发生的是,它将数据库中的所有数据拉入一个DataTable
,对于DataTable
中的每个结果,我搜索AD并在AD的email字段中返回电子邮件地址。同样,当我输入特定的名字和姓氏时,它会起作用,但是当我使用下面的代码时,我只得到一个未处理的异常错误
这是代码。谢谢大家!
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"));
}
}
private void btnRun\u单击(对象发送方,事件参数e)
{
DataTable dt=新的DataTable();
字符串APIdbUser=“dbuser”;
字符串apidserver=“dbserver”;
字符串APIdbUserPW=“dbpassword”;
字符串APIdbDatabase=“thedatabase”;
string TrustedConnection=“否”;
var ConnectionTimeout=“30”;
使用(SqlConnection myConnection=newsqlconnection(“用户id=“+APIdbUser+”;”+
“password=“+APIdbUserPW+”;server=“+APIdbServer+”+
“Trusted_Connection=“+TrustedConnection+”;”+
“database=“+APIdbDatabase+”;”+
“连接超时=“+ConnectionTimeout))
{
使用(var myCommand=new SqlCommand(“从dbo.Users中选择*,其中FirstName!='NULL'和LastName!='NULL'和Gender!='NULL';”,myConnection))
{
myConnection.Open();
使用(SqlDataReader dr=myCommand.ExecuteReader())
{
dt.荷载(dr);
}
myConnection.Close();
}
}
foreach(数据行dr在dt.行中)
{
字符串ldapAddress=“LDAP://url”;
字符串ldapusername=“ldapuser”;
字符串ldappassword=“ldapuser”;
DirectoryEntry de=新的DirectoryEntry(ldapAddress、ldapusername、ldappassword);
DirectorySearcher ds=新的DirectorySearcher(de);
ds.Filter=“(&(&(&(objectCategory=person)(objectClass=user))(givenName=“+dr.Field(“FirstName”)+”)(sn=“+dr.Field(“LastName”)+”)(description=“+dr.Field(“Code”)+”);
ds.SearchScope=SearchScope.Subtree;
SearchResult rs=ds.FindOne();
if(dr.Field(“Code”)==rs.GetDirectoryEntry().Properties[“description”].Value.ToString())
{
MessageBox.Show(“此用户的电子邮件地址为:+rs.GetDirectoryEntry().Properties[“mail”].Value.ToString());
}
//MessageBox.Show(dr.Field(“FirstName”)+“”+dr.Field(“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(rs!=null)
{
if(dr.Field(“Code”)==rs.GetDirectoryEntry().Properties[“description”].Value.ToString())
{
if(rs.GetDirectoryEntry().Properties[“邮件”].Value==null)
{
MessageBox.Show(“此电子邮件不适用”);
}
其他的
{
MessageBox.Show(“此用户的电子邮件地址为:+rs.GetDirectoryEntry().Properties[“mail”].Value.ToString());
}
}
}
必须检查是否为空
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(rs!=null)
{
if(dr.Field(“Code”)==rs.GetDirectoryEntry().Properties[“description”].Value.ToString())
{
if(rs.GetDirectoryEntry().Properties[“邮件”].Value==null)
{
MessageBox.Show(“此电子邮件不适用”);
}
其他的
{
MessageBox.Show(“此用户的电子邮件地址为:+rs.GetDirectoryEntry().Properties[“mail”].Value.ToString());
}
}
}
其中FirstName!='NULL'和LastName!='NULL'和性别!='NULL'
-为什么您要将文本'NULL'
存储在表中,而不是将字段NULL
保留?您遇到了什么错误?在哪一行代码上?“NullReferenceUnhandled”ProjectName中发生类型为“System.NullReferenceException”的未处理异常@Siyual我实际上更希望它是从dbo.users中选择*并保持不变。这真的没关系,我只是犯了个错误。IF语句出错。有大量对象处于IF
状态rs
可能为null,GetDirectoryEntry方法可能返回null,属性数组可能为null,属性的值可能为null。您需要检查导致问题的原因,并在代码中正确处理。因此,在这种情况下,有没有一种方法可以告诉它忽略具有空值的条目?我尝试添加机会来忽略这些,如&&R!=空ect ect,广告中有9000多个用户,如果这些用户中有任何一个有空字段,我就无法通过这些筛选哈哈