Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/265.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# 基于xml的sql查询动态生成_C#_Sql - Fatal编程技术网

C# 基于xml的sql查询动态生成

C# 基于xml的sql查询动态生成,c#,sql,C#,Sql,我需要根据从dropdownlist或文本框中输入或选择的值填充gridview。用户可能没有提到所有的标准。我正在寻找一种根据用户的选择动态生成数据库查询的解决方案。试试这个,您需要根据您的条件更改代码 StringBuilder sqlQuery; SqlConnection con = new SqlConnection(@"Server=myServerName\myInstanceName;Database=myDataBase;User

我需要根据从dropdownlist或文本框中输入或选择的值填充gridview。用户可能没有提到所有的标准。我正在寻找一种根据用户的选择动态生成数据库查询的解决方案。

试试这个,您需要根据您的条件更改代码

            StringBuilder sqlQuery;
            SqlConnection con = new SqlConnection(@"Server=myServerName\myInstanceName;Database=myDataBase;User Id=myUsername;Password=myPassword; ");
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            string join = "";
            // textboxes
            if (TextBox_percentage.Text.Trim() != "")
            {
                sqlQuery.Append(join+" [percentage_column]>=@percentage");
                cmd.Parameters.AddWithValue("@percentage", TextBox_percentage.Text);
                join = " and ";

            }
            if (TextBox_cgpa.Text.Trim() != "")
            {
                sqlQuery.Append(join + " [cgpa_column]>=@cgpascore");
                cmd.Parameters.AddWithValue("@cgpascore", TextBox_cgpa.Text);
                join = " and ";
            }
            if (TextBox_experience.Text.Trim() != "")
            {
                sqlQuery.Append(join + " [experience_column]=@experience");
                cmd.Parameters.AddWithValue("@experience", TextBox_experience.Text);
                join = " and ";
            }
            // dropdowns, 'Default value'? Text/Value?
            if (dropdown_Industry.SelectedItem.Text != "Default Value")
            {

                sqlQuery.Append(join + " [Industry_column]=@Industry");
                cmd.Parameters.AddWithValue("@Industry", dropdown_Industry.SelectedItem.Text);
                join = " and ";
            }
            if (dropdown_Specialization.SelectedItem.Text != "Default Value")
            {

                sqlQuery.Append(join + " [Specialization _column]=@Specialization ");
                cmd.Parameters.AddWithValue("@Specialization ", dropdown_Specialization.SelectedItem.Text);
                join = " and ";
            }
            if (dropdown_Highest_degree.SelectedItem.Text != "Default Value")
            {

                sqlQuery.Append(join + " [Highest_degree_column]=@Highest_degree");
                cmd.Parameters.AddWithValue("@Highest_degree", dropdown_Highest_degree.SelectedItem.Text);
                join = " and ";
            }
            if (dropdown_College.SelectedItem.Text != "Default Value")
            {

                sqlQuery.Append(join + " [College_column]=@College");
                cmd.Parameters.AddWithValue("@College", dropdown_College.SelectedItem.Text);
                join = " and ";
            }
            if (dropdown_Location.SelectedItem.Text != "Default Value")
            {

                sqlQuery.Append(join + " [Location_column]=@Location");
                cmd.Parameters.AddWithValue("@Location", dropdown_Location.SelectedItem.Text);
                join = " and ";
            }

            cmd.CommandText = "Select * from table where " + sqlQuery.ToString();
            con.Open(); // try-catch-finally
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);
            con.Close();
            gridView.DataSource = dt;
            gridView.DataBind();

if语句通常适用于根据条件修改值。以下是标准-高于或等于百分比-文本框高于或等于cgpa-文本框月体验-文本框行业-下拉列表专业化-下拉最高学位-下拉学院-下拉位置-dropdown@SupriyaParate例如你好像有误解了堆栈溢出的目的。这不是一个接受需求并为您发送代码的免费编码服务。我们很乐意通过回答有关错误或意外行为的问题来帮助您编写任何代码。但你必须付出一些努力。