我正在做一个高级搜索代码..我有 6 个下拉列表,用户可以从一个或所有下拉列表中选择一个或多个值,或者选择“-”值,这意味着没有选择值..我的代码正在运行结果是所有值的联合..我怎样才能只找到相交?
我的意思是,如果我从第一个下拉列表中选择 (Asia),从第二个下拉列表中选择 (Arabic),我的结果是所有亚洲国家和所有使用阿拉伯语的国家.. 我怎么能只有亚洲国家会谈阿拉伯语 >> 相交?
if (!Class1.Search_Continent.Equals("-"))//DropDownList1.SelectedValue.ToString();
{
sunc.conn.Open();
SqlCommand cmd1 = new SqlCommand("Select Country_name FROM Country WHERE Continent_name='" + DropDownList1.SelectedValue + "'", sunc.conn);
SqlDataReader dr1;
dr1 = cmd1.ExecuteReader();
while (dr1.Read())
{DropDownList9.Items.Add(dr1["Country_name"].ToString());}
sunc.conn.Close();
if (!Class1.Search_Country.Equals("-"))//DropDownList2.SelectedValue.ToString();
{
RemoveDuplicateItems(DropDownList9);
sunc.conn.Open();
SqlCommand cmd2 = new SqlCommand("Select Country_name FROM Country WHERE Country_name='" + DropDownList2.SelectedValue + "'", sunc.conn);
SqlDataReader dr2;
dr2 = cmd2.ExecuteReader();
while (dr2.Read())
{DropDownList9.Items.Add(dr2["Country_name"].ToString());}
sunc.conn.Close();
if (!Class1.Search_City.Equals("-"))//DropDownList3.SelectedValue.ToString();
{
RemoveDuplicateItems(DropDownList9);
sunc.conn.Open();
SqlCommand cmd3 = new SqlCommand("Select Country_name FROM City WHERE City_name='" + DropDownList3.SelectedValue + "'", sunc.conn);
SqlDataReader dr3;
dr3 = cmd3.ExecuteReader();
while (dr3.Read())
{
DropDownList9.Items.Add(dr3["Country_name"].ToString());
}
//dr3.Close();
//conn3.Close();
sunc.conn.Close();
if (!Class1.Search_Religion.Equals("-"))//DropDownList4.SelectedValue.ToString();
{
RemoveDuplicateItems(DropDownList9);
//SqlConnection conn4 = new SqlConnection(@"Data Source=AK-PC\MSSQLSERVER1;Initial Catalog=DB;Integrated Security=True");
//conn4.Open();
sunc.conn.Open();
SqlCommand cmd4 = new SqlCommand("Select Country_name FROM Religion WHERE Religion_name='" + DropDownList4.SelectedValue + "'", sunc.conn);
SqlDataReader dr4;
dr4 = cmd4.ExecuteReader();
while (dr4.Read())
{
DropDownList9.Items.Add(dr4["Country_name"].ToString());
}
//dr4.Close();
//conn4.Close();
sunc.conn.Close();
if (!Class1.Search_Type.Equals("-"))//DropDownList5.SelectedValue.ToString();
{
RemoveDuplicateItems(DropDownList9);
//SqlConnection conn5 = new SqlConnection(@"Data Source=AK-PC\MSSQLSERVER1;Initial Catalog=DB;Integrated Security=True");
//conn5.Open();
sunc.conn.Open();
SqlCommand cmd5 = new SqlCommand("Select Country_name FROM Country WHERE Type_of_government='" + DropDownList5.SelectedValue + "'", sunc.conn);
SqlDataReader dr5;
dr5 = cmd5.ExecuteReader();
while (dr5.Read())
{
DropDownList9.Items.Add(dr5["Country_name"].ToString());
}
//dr5.Close();
//conn5.Close();
sunc.conn.Close();
if (!Class1.Search_Language.Equals("-"))//DropDownList6.SelectedValue.ToString();
{
RemoveDuplicateItems(DropDownList9);
//SqlConnection conn6 = new SqlConnection(@"Data Source=AK-PC\MSSQLSERVER1;Initial Catalog=DB;Integrated Security=True");
//conn6.Open();
sunc.conn.Open();
SqlCommand cmd6 = new SqlCommand("Select Country_name FROM Language WHERE Language_name='" + DropDownList6.SelectedValue + "'", sunc.conn);
SqlDataReader dr6;
dr6 = cmd6.ExecuteReader();
while (dr6.Read())
{
DropDownList9.Items.Add(dr6["Country_name"].ToString());
}
//dr6.Close();
//conn6.Close();
sunc.conn.Close();
if (DropDownList1.SelectedValue.Equals("-") && DropDownList2.SelectedValue.Equals("-") &&
DropDownList3.SelectedValue.Equals("-") && DropDownList4.SelectedValue.Equals("-") &&
DropDownList5.SelectedValue.Equals("-") && DropDownList6.SelectedValue.Equals("-"))
{
Button2.Enabled = false;
Label1.Text = "you have to choose from the dropdown list";
}
else if (DropDownList9.SelectedValue.Equals("-"))
{
Button2.Enabled = false;
Label1.Text = "No result ";
}
}
}
}
}
}
}