0

我正在开发一个查询个人电影数据库的程序。计划的难点在于选择流派。例如,假设我有 3 个复选框动作、冒险和喜剧。一部电影可能属于一种类型或多种类型。电影可以是动作片或动作/喜剧。

下面的代码片段是我开始的......但不适应所有的变化。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);
4

3 回答 3

3

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

if (chk_Adv.Checked == true) {
   str_SQL = str_SQL+" AND Fantasy = 1"
}
于 2013-03-22T13:30:32.173 回答
1

如果所有列数据类型都bit在 sql 中,请尝试此操作:

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,请在此处查看

于 2013-03-22T13:54:47.547 回答
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);
于 2013-03-22T21:25:22.603 回答