基于复选框的查询

时间:2013-03-22 13:25:18

标签: c# ms-access

我正在研究一个可以查询个人电影数据库的程序。 计划中的皱纹是选择流派。 例如,假设我有3个复选框Action,Adventure和Comedy。 电影可能属于一种类型或多种类型。 电影可以是动作或动作/喜剧。

下面的代码段是我开始使用的...但不适用于所有变体。 Fill_Grid函数填充dataGridView。如果电影具有一种类型或者用户选择与电影在数据库中具有的完全相同的类型,则它可以工作。 需要发生的是,如果一部电影是动作喜剧,它应该出现,如果用户只选择动作或只选择喜剧或动作和喜剧。

如果没有意义,请提问。

提前致谢, -J

       string str_SQL = "SELECT Name, File, Action, Fantasy, Horror, Thriller, Adventure, Animation, Comedy, Crime, Documentary, Drama, Family, Games, Mystery, Romance, SciFi, War FROM tbl_Main WHERE Action = " + chk_Action.Checked +
            " AND Fantasy = " + chk_Adv.Checked +
            " AND Horror = " + chk_Hor.Checked +
            " AND Thriller = " + chk_Thrill.Checked +
            " AND Adventure = " + chk_Adv.Checked +
            " AND Animation = " + chk_Anim.Checked +
            " AND Comedy = " + chk_Com.Checked +
            " AND Crime = " + chk_Crime.Checked +
            " AND Documentary = " + chk_Doc.Checked +
            " AND Drama = " + chk_Drama.Checked +
            " AND Family = " + chk_Fam.Checked +
            " AND Games = " + chk_Game.Checked +
            " AND Mystery = " + chk_Mys.Checked +
            " AND Romance = " + chk_Rom.Checked +
            " AND SciFi = " + chk_Sci.Checked +
            " AND War = " + chk_War.Checked + ";";

            Fill_Grid(str_SQL);

3 个答案:

答案 0 :(得分:2)

查看是否先选中该复选框。然后,将其添加到查询中。

if (chk_Adv.Checked == true) {
   str_SQL = str_SQL+" AND Fantasy = 1"
}

答案 1 :(得分:0)

如果sql:

中的所有列数据类型都是bit,请尝试此操作
string str_SQL = "SELECT Name, File, Action, Fantasy, Horror, Thriller, Adventure, Animation, Comedy, Crime, Documentary, Drama, Family, Games, Mystery, Romance, SciFi, War FROM tbl_Main WHERE Action = " + chk_Action.Checked +
" AND Fantasy = " + chk_Adv.Checked?-1 :0 +
" AND Horror = " + chk_Hor.Checked ? -1 : 0 +
" AND Thriller = " + chk_Thrill.Checked ? -1 : 0 +
" AND Adventure = " + chk_Adv.Checked ? -1 : 0 +
" AND Animation = " + chk_Anim.Checked ? -1 : 0 +
" AND Comedy = " + chk_Com.Checked ? -1 : 0 +
" AND Crime = " + chk_Crime.Checked ? -1 : 0 +
" AND Documentary = " + chk_Doc.Checked ? -1 : 0 +
" AND Drama = " + chk_Drama.Checked ? -1 : 0 +
" AND Family = " + chk_Fam.Checked ? -1 : 0 +
" AND Games = " + chk_Game.Checked ? -1 : 0 +
" AND Mystery = " + chk_Mys.Checked ? -1 : 0 +
" AND Romance = " + chk_Rom.Checked ? -1 : 0 +
" AND SciFi = " + chk_Sci.Checked ? -1 : 0 +
" AND War = " + chk_War.Checked ? -1 : 0 + ";";

Fill_Grid(str_SQL);

<强>更新 如果您使用ms-access且数据类型为Yes/No,则true = -1且false = 0,check here

答案 2 :(得分:0)

下面的代码段有效。感谢大家的投入,让我朝着正确的方向前进

string str_Checkboxes="";
string str_SQL = "SELECT Name, File, Action, Fantasy, Horror, Thriller, Adventure, Animation, Comedy, Crime, Documentary, Drama, Family, Games, Mystery, Romance, SciFi, War FROM tbl_Main WHERE ";

//Loop through each control on the form, we are looking for checkboxes
foreach (Control c in this.Controls)
 {
  if(c is CheckBox)
    {
      if (((CheckBox)c).Checked)
        {
         //Bypass putting AND at the beginning of str_Checkboxes
         if(str_Checkboxes != "")
           str_Checkboxes+=" AND ";
         //Checkbox text is the same as the field name in the database
           str_Checkboxes += (((CheckBox)c).Text) + " = True";
        }
     }
   }
//build the SQL
str_SQL += str_Checkboxes + ";"; 
//Fill the grid
Fill_Grid(str_SQL);