基于

2014-09-01 50 views
-7
动态生成sql查询

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

+1

'if'语句通常适用于根据条件修改值。 – David 2014-09-01 16:00:48

+0

以下是criterias- 高于或等于百分之 - 文本 高于或等于CGPA - 文本 经验(月) - 文本 工业 - 下拉列表 专精 - 下拉 最高学位 - 下拉 学院 - 下拉 位置 - dropdown – 2014-09-01 16:56:07

+0

@SupriyaParate:你似乎误解了Stack Overflow的目的。这不是免费的编码服务,它接受要求并为您发布代码。我们很高兴通过回答有关错误或意外行为的问题来帮助您编写任何代码。但是你必须实际付出努力。 – David 2014-09-01 17:37:27

回答

0

试试这个,你将需要根据你的条件改变代码。

  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]>[email protected]"); 
       cmd.Parameters.AddWithValue("@percentage", TextBox_percentage.Text); 
       join = " and "; 

      } 
      if (TextBox_cgpa.Text.Trim() != "") 
      { 
       sqlQuery.Append(join + " [cgpa_column]>[email protected]"); 
       cmd.Parameters.AddWithValue("@cgpascore", TextBox_cgpa.Text); 
       join = " and "; 
      } 
      if (TextBox_experience.Text.Trim() != "") 
      { 
       sqlQuery.Append(join + " [experience_column][email protected]"); 
       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][email protected]"); 
       cmd.Parameters.AddWithValue("@Industry", dropdown_Industry.SelectedItem.Text); 
       join = " and "; 
      } 
      if (dropdown_Specialization.SelectedItem.Text != "Default Value") 
      { 

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

       sqlQuery.Append(join + " [Highest_degree_column][email protected]_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][email protected]"); 
       cmd.Parameters.AddWithValue("@College", dropdown_College.SelectedItem.Text); 
       join = " and "; 
      } 
      if (dropdown_Location.SelectedItem.Text != "Default Value") 
      { 

       sqlQuery.Append(join + " [Location_column][email protected]"); 
       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();