Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用下拉列表C进行高级搜索#_C#_Asp.net_Sql - Fatal编程技术网

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

C# 使用下拉列表C进行高级搜索#,c#,asp.net,sql,C#,Asp.net,Sql,我正在做一个高级搜索代码。。我有6个下拉列表,用户可以从一个或所有下拉列表中选择一个或多个值,或者选择“-”值,这表示未选择任何值。。我的代码正在运行,结果是所有值的并集。。我怎么才能只找到交集 if (!Class1.Search_Continent.Equals("-"))//DropDownList1.SelectedValue.ToString(); { sunc.conn.Open(); SqlCommand cmd1 =

我正在做一个高级搜索代码。。我有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 ";
                                }
                            }
                        }
                    }
                }
            }
        }
我的意思是,如果我从第一个下拉列表中选择(亚洲),从第二个下拉列表中选择(阿拉伯语),我的结果是所有亚洲国家和所有使用阿拉伯语的国家。。 我怎么能只让那些说阿拉伯语的亚洲国家在互联网上

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

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

例如:

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);

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

例如:

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);

您必须修改查询,如下所示

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);

希望这有帮助

您必须修改查询,如下所示

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);

希望这有帮助

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

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、@religation和@Government作为参数传递给查询。如果传入任何单个参数,则WHERE子句将对其进行过滤;如果该参数为空,则忽略。

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

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、@religation和@Government作为参数传递给查询。如果传入任何单个参数,则WHERE子句将对其进行过滤;或者,如果该参数为空,则忽略它。

我只能说,我很高兴我不必维护它;)听起来你需要重构代码,也许还需要创建一个存储过程,你需要一些Where子句,你需要过滤,但是你最终需要基于所有选项构建查询,而不是一次一个。我能说的是,我很高兴我不必维护它;)听起来你需要重构代码,也许还需要创建一个存储过程,你需要一些Where子句,你需要进行筛选,但是你需要根据最后的所有选项构建查询,而不是一次一个,你能解释得更多吗?你能解释得更多吗?但是,如果用户留下一些下拉列表而没有选择值怎么办?但是,如果用户留下一些下拉列表而没有选择值怎么办?@user1915452:这是SQL代码。。。将其用作命令,然后将参数添加到命令对象中necessary@user1915452:这是SQL代码。。。将其用作命令,然后根据需要向命令对象添加参数