按复选框类别筛选查询

时间:2014-02-08 07:08:31

标签: c# sql checkbox where-clause

我有一个包含四个列的数据库表。 这些包含问题1,2,3和4的answerOption。 answerOption可以是1到5之间的数字。

我希望有一个包含20个复选框的表,所以我可以说Q1必须是1或4而Q4必须是3。

我想我必须制作某种OnChecked事件(使用ASP.NET),然后构造Where子句。我的挑战就是这个条款。有什么建议吗?

修改

我可以给出一个小案例,以便每个人都理解我实际上想要实现的目标。我有一个调查有四个问题和五个不同的答案选项。调查如下:

  1. 你多大了?
  2. 你住在哪个国家?
  3. 你有几个孩子?
  4. 你有多少收入?
  5. 我想在每个饼图中显示每个问题,默认情况下,应显示所有内容。通过演示,您可以看到一个人的年龄,一个人住在哪里,一个人有多少孩子和收入的可能性。

    但是我希望有复选框,这样我才能在全国范围内看到一个收入不错的40岁的人。并且看看他/她将有多少孩子的概率。

    这个工具的目标也是找到糟糕情况的目标。

    这是清楚还是?

1 个答案:

答案 0 :(得分:1)

下面是使用基于CheckBox的过滤的示例。 WHERE子句有点长。

SELECT * FROM Answers WHERE
(
    @Q1AnswerOption1 | @Q1AnswerOption2 | @Q1AnswerOption3 | @Q1AnswerOption3 | @Q1AnswerOption4 | @Q1AnswerOption5 = 0
    OR Q1Answer = 1 * @Q1AnswerOption1 OR Q1Answer = 2 * @Q1AnswerOption2 OR Q1Answer = 3 * @Q1AnswerOption3 OR Q1Answer = 4 * @Q1AnswerOption4 OR Q1Answer = 5 * @Q1AnswerOption5
) AND (
    @Q2AnswerOption1 | @Q2AnswerOption2 | @Q2AnswerOption3 | @Q2AnswerOption3 | @Q2AnswerOption4 | @Q2AnswerOption5 = 0
    OR Q2Answer = 1 * @Q2AnswerOption1 OR Q2Answer = 2 * @Q2AnswerOption2 OR Q2Answer = 3 * @Q2AnswerOption3 OR Q2Answer = 4 * @Q2AnswerOption4 OR Q3Answer = 5 * @Q2AnswerOption5
) AND (
    @Q3AnswerOption1 | @Q3AnswerOption2 | @Q3AnswerOption3 | @Q3AnswerOption3 | @Q3AnswerOption4 | @Q3AnswerOption5 = 0
    OR Q3Answer = 1 * @Q3AnswerOption1 OR Q3Answer = 2 * @Q3AnswerOption2 OR Q3Answer = 3 * @Q3AnswerOption3 OR Q3Answer = 4 * @Q3AnswerOption4 OR Q3Answer = 5 * @Q3AnswerOption5
) AND (
    @Q4AnswerOption1 | @Q4AnswerOption2 | @Q4AnswerOption3 | @Q4AnswerOption3 | @Q4AnswerOption4 | @Q4AnswerOption5 = 0
    OR Q4Answer = 1 * @Q4AnswerOption1 OR Q4Answer = 2 * @Q4AnswerOption2 OR Q4Answer = 3 * @Q4AnswerOption3 OR Q4Answer = 4 * @Q4AnswerOption4 OR Q4Answer = 5 * @Q4AnswerOption5
)

修改 第一部分@ Q1AnswerOption1 | @ Q1AnswerOption2 | @ Q1AnswerOption3 | @ Q1AnswerOption3 | @ Q1AnswerOption4 |取消选中所有Q1复选框时,@ Q1AnswerOption5 = 0为真。在这种情况下,无论Q1Answer是什么,都会选择行。如果选中至少一个复选框,则第一部分为false,第二部分仅选择匹配的行。


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Form1.aspx.cs" Inherits="aspx_Form1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT * FROM Answers WHERE (@Q1AnswerOption1 | @Q1AnswerOption2 | @Q1AnswerOption3 | @Q1AnswerOption3 | @Q1AnswerOption4 | @Q1AnswerOption5 = 0 OR Q1Answer = 1 * @Q1AnswerOption1 OR Q1Answer = 2 * @Q1AnswerOption2 OR Q1Answer = 3 * @Q1AnswerOption3 OR Q1Answer = 4 * @Q1AnswerOption4 OR Q1Answer = 5 * @Q1AnswerOption5) AND (@Q2AnswerOption1 | @Q2AnswerOption2 | @Q2AnswerOption3 | @Q2AnswerOption3 | @Q2AnswerOption4 | @Q2AnswerOption5 = 0 OR Q2Answer = 1 * @Q2AnswerOption1 OR Q2Answer = 2 * @Q2AnswerOption2 OR Q2Answer = 3 * @Q2AnswerOption3 OR Q2Answer = 4 * @Q2AnswerOption4 OR Q3Answer = 5 * @Q2AnswerOption5) AND (@Q3AnswerOption1 | @Q3AnswerOption2 | @Q3AnswerOption3 | @Q3AnswerOption3 | @Q3AnswerOption4 | @Q3AnswerOption5 = 0 OR Q3Answer = 1 * @Q3AnswerOption1 OR Q3Answer = 2 * @Q3AnswerOption2 OR Q3Answer = 3 * @Q3AnswerOption3 OR Q3Answer = 4 * @Q3AnswerOption4 OR Q3Answer = 5 * @Q3AnswerOption5) AND (@Q4AnswerOption1 | @Q4AnswerOption2 | @Q4AnswerOption3 | @Q4AnswerOption3 | @Q4AnswerOption4 | @Q4AnswerOption5 = 0 OR Q4Answer = 1 * @Q4AnswerOption1 OR Q4Answer = 2 * @Q4AnswerOption2 OR Q4Answer = 3 * @Q4AnswerOption3 OR Q4Answer = 4 * @Q4AnswerOption4 OR Q4Answer = 5 * @Q4AnswerOption5)">
                <SelectParameters>
                    <asp:ControlParameter ControlID="Q1AnswerOption1CheckBox" Name="Q1AnswerOption1" PropertyName="Checked" Type="Byte" DefaultValue="0" />
                    <asp:ControlParameter ControlID="Q1AnswerOption2CheckBox" Name="Q1AnswerOption2" PropertyName="Checked" Type="Byte" DefaultValue="0" />
                    <asp:ControlParameter ControlID="Q1AnswerOption3CheckBox" Name="Q1AnswerOption3" PropertyName="Checked" Type="Byte" DefaultValue="0" />
                    <asp:ControlParameter ControlID="Q1AnswerOption4CheckBox" Name="Q1AnswerOption4" PropertyName="Checked" Type="Byte" DefaultValue="0" />
                    <asp:ControlParameter ControlID="Q1AnswerOption5CheckBox" Name="Q1AnswerOption5" PropertyName="Checked" Type="Byte" DefaultValue="0" />

                    <asp:ControlParameter ControlID="Q2AnswerOption1CheckBox" Name="Q2AnswerOption1" PropertyName="Checked" Type="Byte" DefaultValue="0" />
                    <asp:ControlParameter ControlID="Q2AnswerOption2CheckBox" Name="Q2AnswerOption2" PropertyName="Checked" Type="Byte" DefaultValue="0" />
                    <asp:ControlParameter ControlID="Q2AnswerOption3CheckBox" Name="Q2AnswerOption3" PropertyName="Checked" Type="Byte" DefaultValue="0" />
                    <asp:ControlParameter ControlID="Q2AnswerOption4CheckBox" Name="Q2AnswerOption4" PropertyName="Checked" Type="Byte" DefaultValue="0" />
                    <asp:ControlParameter ControlID="Q2AnswerOption5CheckBox" Name="Q2AnswerOption5" PropertyName="Checked" Type="Byte" DefaultValue="0" />

                    <asp:ControlParameter ControlID="Q3AnswerOption1CheckBox" Name="Q3AnswerOption1" PropertyName="Checked" Type="Byte" DefaultValue="0" />
                    <asp:ControlParameter ControlID="Q3AnswerOption2CheckBox" Name="Q3AnswerOption2" PropertyName="Checked" Type="Byte" DefaultValue="0" />
                    <asp:ControlParameter ControlID="Q3AnswerOption3CheckBox" Name="Q3AnswerOption3" PropertyName="Checked" Type="Byte" DefaultValue="0" />
                    <asp:ControlParameter ControlID="Q3AnswerOption4CheckBox" Name="Q3AnswerOption4" PropertyName="Checked" Type="Byte" DefaultValue="0" />
                    <asp:ControlParameter ControlID="Q3AnswerOption5CheckBox" Name="Q3AnswerOption5" PropertyName="Checked" Type="Byte" DefaultValue="0" />

                    <asp:ControlParameter ControlID="Q4AnswerOption1CheckBox" Name="Q4AnswerOption1" PropertyName="Checked" Type="Byte" DefaultValue="0" />
                    <asp:ControlParameter ControlID="Q4AnswerOption2CheckBox" Name="Q4AnswerOption2" PropertyName="Checked" Type="Byte" DefaultValue="0" />
                    <asp:ControlParameter ControlID="Q4AnswerOption3CheckBox" Name="Q4AnswerOption3" PropertyName="Checked" Type="Byte" DefaultValue="0" />
                    <asp:ControlParameter ControlID="Q4AnswerOption4CheckBox" Name="Q4AnswerOption4" PropertyName="Checked" Type="Byte" DefaultValue="0" />
                    <asp:ControlParameter ControlID="Q4AnswerOption5CheckBox" Name="Q4AnswerOption5" PropertyName="Checked" Type="Byte" DefaultValue="0" />
                </SelectParameters>
            </asp:SqlDataSource>
            <br />
            Question 1&nbsp;&nbsp;&nbsp;
            <asp:CheckBox ID="Q1AnswerOption1CheckBox" runat="server" Text="AnswerOption1" AutoPostBack="True" />
            &nbsp;&nbsp;&nbsp;
            <asp:CheckBox ID="Q1AnswerOption2CheckBox" runat="server" Text="AnswerOption2" AutoPostBack="True" />
            &nbsp;&nbsp;&nbsp;
            <asp:CheckBox ID="Q1AnswerOption3CheckBox" runat="server" Text="AnswerOption3" AutoPostBack="True" />
            &nbsp;&nbsp;&nbsp;
            <asp:CheckBox ID="Q1AnswerOption4CheckBox" runat="server" Text="AnswerOption4" AutoPostBack="True" />
            &nbsp;&nbsp;&nbsp;
            <asp:CheckBox ID="Q1AnswerOption5CheckBox" runat="server" Text="AnswerOption5" AutoPostBack="True" />
            <br />
            Question 2&nbsp;&nbsp;&nbsp;
            <asp:CheckBox ID="Q2AnswerOption1CheckBox" runat="server" Text="AnswerOption1" AutoPostBack="True" />
            &nbsp;&nbsp;&nbsp;
            <asp:CheckBox ID="Q2AnswerOption2CheckBox" runat="server" Text="AnswerOption2" AutoPostBack="True" />
            &nbsp;&nbsp;&nbsp;
            <asp:CheckBox ID="Q2AnswerOption3CheckBox" runat="server" Text="AnswerOption3" AutoPostBack="True" />
            &nbsp;&nbsp;&nbsp;
            <asp:CheckBox ID="Q2AnswerOption4CheckBox" runat="server" Text="AnswerOption4" AutoPostBack="True" />
            &nbsp;&nbsp;&nbsp;
            <asp:CheckBox ID="Q2AnswerOption5CheckBox" runat="server" Text="AnswerOption5" AutoPostBack="True" />
            <br />

            Question 3&nbsp;&nbsp;&nbsp;
            <asp:CheckBox ID="Q3AnswerOption1CheckBox" runat="server" Text="AnswerOption1" AutoPostBack="True" />
            &nbsp;&nbsp;&nbsp;
            <asp:CheckBox ID="Q3AnswerOption2CheckBox" runat="server" Text="AnswerOption2" AutoPostBack="True" />
            &nbsp;&nbsp;&nbsp;
            <asp:CheckBox ID="Q3AnswerOption3CheckBox" runat="server" Text="AnswerOption3" AutoPostBack="True" />
            &nbsp;&nbsp;&nbsp;
            <asp:CheckBox ID="Q3AnswerOption4CheckBox" runat="server" Text="AnswerOption4" AutoPostBack="True" />
            &nbsp;&nbsp;&nbsp;
            <asp:CheckBox ID="Q3AnswerOption5CheckBox" runat="server" Text="AnswerOption5" AutoPostBack="True" />
            <br />

            Question 4&nbsp;&nbsp;&nbsp;
            <asp:CheckBox ID="Q4AnswerOption1CheckBox" runat="server" Text="AnswerOption1" AutoPostBack="True" />
            &nbsp;&nbsp;&nbsp;
            <asp:CheckBox ID="Q4AnswerOption2CheckBox" runat="server" Text="AnswerOption2" AutoPostBack="True" />
            &nbsp;&nbsp;&nbsp;
            <asp:CheckBox ID="Q4AnswerOption3CheckBox" runat="server" Text="AnswerOption3" AutoPostBack="True" />
            &nbsp;&nbsp;&nbsp;
            <asp:CheckBox ID="Q4AnswerOption4CheckBox" runat="server" Text="AnswerOption4" AutoPostBack="True" />
            &nbsp;&nbsp;&nbsp;
            <asp:CheckBox ID="Q4AnswerOption5CheckBox" runat="server" Text="AnswerOption5" AutoPostBack="True" />
            <br />

            <br />
            Answers<br />
            <asp:ListView ID="ListView1" runat="server" DataKeyNames="Id" DataSourceID="SqlDataSource1">
                <AlternatingItemTemplate>
                    <tr style="background-color: #FFF8DC;">
                        <td>
                            <asp:Label ID="IdLabel" runat="server" Text='<%# Eval("Id") %>' />
                        </td>
                        <td>
                            <asp:Label ID="Q1AnswerLabel" runat="server" Text='<%# Eval("Q1Answer") %>' />
                        </td>
                        <td>
                            <asp:Label ID="Q2AnswerLabel" runat="server" Text='<%# Eval("Q2Answer") %>' />
                        </td>
                        <td>
                            <asp:Label ID="Q3AnswerLabel" runat="server" Text='<%# Eval("Q3Answer") %>' />
                        </td>
                        <td>
                            <asp:Label ID="Q4AnswerLabel" runat="server" Text='<%# Eval("Q4Answer") %>' />
                        </td>
                    </tr>
                </AlternatingItemTemplate>
                <EditItemTemplate>
                    <tr style="background-color: #008A8C; color: #FFFFFF;">
                        <td>
                            <asp:Button ID="UpdateButton" runat="server" CommandName="Update" Text="Update" />
                            <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Cancel" />
                        </td>
                        <td>
                            <asp:Label ID="IdLabel1" runat="server" Text='<%# Eval("Id") %>' />
                        </td>
                        <td>
                            <asp:TextBox ID="Q1AnswerTextBox" runat="server" Text='<%# Bind("Q1Answer") %>' />
                        </td>
                        <td>
                            <asp:TextBox ID="Q2AnswerTextBox" runat="server" Text='<%# Bind("Q2Answer") %>' />
                        </td>
                        <td>
                            <asp:TextBox ID="Q3AnswerTextBox" runat="server" Text='<%# Bind("Q3Answer") %>' />
                        </td>
                        <td>
                            <asp:TextBox ID="Q4AnswerTextBox" runat="server" Text='<%# Bind("Q4Answer") %>' />
                        </td>
                    </tr>
                </EditItemTemplate>
                <EmptyDataTemplate>
                    <table runat="server" style="background-color: #FFFFFF; border-collapse: collapse; border-color: #999999; border-style: none; border-width: 1px;">
                        <tr>
                            <td>No data was returned.</td>
                        </tr>
                    </table>
                </EmptyDataTemplate>
                <InsertItemTemplate>
                    <tr style="">
                        <td>
                            <asp:Button ID="InsertButton" runat="server" CommandName="Insert" Text="Insert" />
                            <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Clear" />
                        </td>
                        <td>
                            <asp:TextBox ID="IdTextBox" runat="server" Text='<%# Bind("Id") %>' />
                        </td>
                        <td>
                            <asp:TextBox ID="Q1AnswerTextBox" runat="server" Text='<%# Bind("Q1Answer") %>' />
                        </td>
                        <td>
                            <asp:TextBox ID="Q2AnswerTextBox" runat="server" Text='<%# Bind("Q2Answer") %>' />
                        </td>
                        <td>
                            <asp:TextBox ID="Q3AnswerTextBox" runat="server" Text='<%# Bind("Q3Answer") %>' />
                        </td>
                        <td>
                            <asp:TextBox ID="Q4AnswerTextBox" runat="server" Text='<%# Bind("Q4Answer") %>' />
                        </td>
                    </tr>
                </InsertItemTemplate>
                <ItemTemplate>
                    <tr style="background-color: #DCDCDC; color: #000000;">
                        <td>
                            <asp:Label ID="IdLabel" runat="server" Text='<%# Eval("Id") %>' />
                        </td>
                        <td>
                            <asp:Label ID="Q1AnswerLabel" runat="server" Text='<%# Eval("Q1Answer") %>' />
                        </td>
                        <td>
                            <asp:Label ID="Q2AnswerLabel" runat="server" Text='<%# Eval("Q2Answer") %>' />
                        </td>
                        <td>
                            <asp:Label ID="Q3AnswerLabel" runat="server" Text='<%# Eval("Q3Answer") %>' />
                        </td>
                        <td>
                            <asp:Label ID="Q4AnswerLabel" runat="server" Text='<%# Eval("Q4Answer") %>' />
                        </td>
                    </tr>
                </ItemTemplate>
                <LayoutTemplate>
                    <table runat="server">
                        <tr runat="server">
                            <td runat="server">
                                <table id="itemPlaceholderContainer" runat="server" border="1" style="background-color: #FFFFFF; border-collapse: collapse; border-color: #999999; border-style: none; border-width: 1px; font-family: Verdana, Arial, Helvetica, sans-serif;">
                                    <tr runat="server" style="background-color: #DCDCDC; color: #000000;">
                                        <th runat="server">Id</th>
                                        <th runat="server">Q1Answer</th>
                                        <th runat="server">Q2Answer</th>
                                        <th runat="server">Q3Answer</th>
                                        <th runat="server">Q4Answer</th>
                                    </tr>
                                    <tr id="itemPlaceholder" runat="server">
                                    </tr>
                                </table>
                            </td>
                        </tr>
                        <tr runat="server">
                            <td runat="server" style="text-align: center; background-color: #CCCCCC; font-family: Verdana, Arial, Helvetica, sans-serif; color: #000000;"></td>
                        </tr>
                    </table>
                </LayoutTemplate>
                <SelectedItemTemplate>
                    <tr style="background-color: #008A8C; font-weight: bold; color: #FFFFFF;">
                        <td>
                            <asp:Label ID="IdLabel" runat="server" Text='<%# Eval("Id") %>' />
                        </td>
                        <td>
                            <asp:Label ID="Q1AnswerLabel" runat="server" Text='<%# Eval("Q1Answer") %>' />
                        </td>
                        <td>
                            <asp:Label ID="Q2AnswerLabel" runat="server" Text='<%# Eval("Q2Answer") %>' />
                        </td>
                        <td>
                            <asp:Label ID="Q3AnswerLabel" runat="server" Text='<%# Eval("Q3Answer") %>' />
                        </td>
                        <td>
                            <asp:Label ID="Q4AnswerLabel" runat="server" Text='<%# Eval("Q4Answer") %>' />
                        </td>
                    </tr>
                </SelectedItemTemplate>
            </asp:ListView>

        </div>
    </form>
</body>
</html>