使用下拉列表C#进行高级搜索

时间:2013-01-07 21:37:31

标签: c# asp.net sql

我正在做一个预先搜索代码..我有6个下拉列表,用户可以从一个或所有下拉列表中选择一个或多个值,或选择“ - ”值,这意味着没有选择值..我的代码工作,结果是所有值的并集..我怎么才能找到交叉?

我的意思是,如果我从第一个下拉菜单中选择(亚洲),从第二个下拉菜单选择(阿拉伯语),我的结果是亚洲所有国家和所有拥有阿拉伯语言的国家。 我怎么能只有谈阿拉伯语的亚洲国家>>相交?

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 ";
                                }
                            }
                        }
                    }
                }
            }
        }

3 个答案:

答案 0 :(得分:2)

我会改变你的代码,以便它根据不同的选项创建一个查询,然后只返回该查询的结果。

例如:

string query = "Select Country_name FROM Country WHERE Continent_name='" + DropDownList1.SelectedValue + "'";
if (!Class1.Search_Country.Equals("-"))
   query+= " and Country_name='" + DropDownList2.SelectedValue + "'";

SqlCommand cmd1 = new SqlCommand(query, sunc.conn);

答案 1 :(得分:2)

通常,您希望在单个查询中执行此操作,例如:

SELECT Country_Name
FROM Country C
    INNER JOIN City CTY on (CTY.Country_Name = C.Country_Name)
    INNER JOIN Religion R on (R.Country_Name = C.Country_Name
WHERE ((@City ='') or (CTY.City_Name = @City))
  AND ((@Religion ='') or (R.Religion_Name = @Religion))
  AND ((@Government = '') or (C.Type_of_Government = @Government))

然后,您将@City,@ Rigigion和@Government作为参数传递给查询。如果传入任何单个参数,那么WHERE子句将对其进行过滤;或忽略该参数是否为空。

答案 2 :(得分:0)

您必须修改如下所示的查询。

SqlCommand cmd1 = new SqlCommand("Select Country_name FROM Country WHERE (Continent_name='" + DropDownList1.SelectedValue + "' or Continent_name=Continent_name) AND (Country_name='" + DropDownList2.SelectedValue + "' OR Country_name=Country_name) AND (City_name='" + DropDownList3.SelectedValue + "' OR City_name=City_name) AND (Religion_name='" + DropDownList4.SelectedValue + "' OR Religion_name=Religion_name) AND (Type_of_government='" + DropDownList5.SelectedValue + "' OR Type_of_government=Type_of_government) AND (Language_name='" + DropDownList6.SelectedValue + "' OR Language_name=Language_name)", sunc.conn);

希望这有助于!!

相关问题