SQL Server 2008使用复选框过滤查询

时间:2014-09-27 09:01:50

标签: c# sql asp.net sql-server sql-server-2008

我有5个CheckBox,我想使用Star Rating复选框在gridview中过滤数据, 我希望如果用户选中CheckBox1然后它显示Star Raing 1的数据,用户选择CheckBox1和CheckBox2,那么它也显示星级1和2的数据。我不想使用C#Code绑定GridView数据。

<asp:CheckBox ID="CheckBox5" Text="5" runat="server" AutoPostBack="true" />
<asp:CheckBox ID="CheckBox4" Text="4" runat="server" AutoPostBack="true" />
<asp:CheckBox ID="CheckBox3" Text="3" runat="server" AutoPostBack="true" />
<asp:CheckBox ID="CheckBox2" Text="2" runat="server" AutoPostBack="true" />
<asp:CheckBox ID="CheckBox1" Text="1" runat="server" AutoPostBack="true" />

<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource4" ShowHeader="False" AllowPaging="True"
AllowSorting="True" CellPadding="4" ForeColor="#333333" GridLines="None">
<AlternatingRowStyle BackColor="White" />
<Columns>
    <asp:TemplateField>
        <ItemTemplate>
            <telerik:RadRating ID="RadRating1" Runat="server" DbValue='<%# Bind("star_rating") %>'>
            </telerik:RadRating>
        </ItemTemplate>
    </asp:TemplateField>
</Columns>
<EditRowStyle BackColor="#7C6F57" />
<FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#E3EAEB" />
<SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F8FAFA" />
<SortedAscendingHeaderStyle BackColor="#246B61" />
<SortedDescendingCellStyle BackColor="#D4DFE1" />
<SortedDescendingHeaderStyle BackColor="#15524A" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource4" runat="server" ConnectionString="<%$ ConnectionStrings:travelconn %>"
SelectCommand="SELECT * FROM Table_P tp inner join Table_D td ON tp.id= td.id where
td.star_rating in (@star1,@star2,@star3,@star4,@star5)">
<SelectParameters>
<asp:ControlParameter ControlID="CheckBox1" Name="star1" PropertyName="Checked" />
<asp:ControlParameter ControlID="CheckBox2" Name="star2" PropertyName="Checked" />
<asp:ControlParameter ControlID="CheckBox3" Name="star3" PropertyName="Checked" />
<asp:ControlParameter ControlID="CheckBox4" Name="star4" PropertyName="Checked" />
<asp:ControlParameter ControlID="CheckBox5" Name="star5" PropertyName="Checked" />
</SelectParameters>
</asp:SqlDataSource>

我正在使用ASP.Net C#SQL Server 2008

2 个答案:

答案 0 :(得分:0)

您需要在数据库中创建一个拆分函数,

分割函数的定义

CREATE FUNCTION [dbo].[split]
    (
      @delimited NVARCHAR(MAX),
      @delimiter NVARCHAR(100)
    ) 
 RETURNS @t TABLE (id INT IDENTITY(1,1), val NVARCHAR(MAX))
AS
BEGIN
  DECLARE @xml XML
  SET @xml = N'<t>' + REPLACE(@delimited,@delimiter,'</t><t>') + '</t>'

  INSERT INTO @t(val)
  SELECT  r.value('.','varchar(MAX)') as item
  FROM  @xml.nodes('/t') as records(r)
  RETURN
END

存储过程

然后你需要创建一个存储过程,它将动态构建sql查询并使用这个split函数从你的下拉列表中处理多个逗号分隔列表值。

CREATE PROCEDURE GetData
    @DropDown1  VARCHAR(1000) = NULL,
    @DropDown2  VARCHAR(1000) = NULL,
    @DropDown3  VARCHAR(1000) = NULL
AS
BEGIN
 SET NOCOUNT ON;

 DECLARE @SQL NVARCHAR(MAX);

 SET @SQL = N' SELECT * FROM TableName WHERE 1 = 2 '
            + CASE WHEN @DropDown1 IS NOT NULL 
                THEN N' OR ColumnName1 IN (SELECT Val FROM dbo.split(@DropDown1 )) '
                    ELSE N'' END
            + CASE WHEN @DropDown2 IS NOT NULL 
                THEN N' OR ColumnName2 IN (SELECT Val FROM dbo.split(@DropDown2)) '
                    ELSE N'' END
            + CASE WHEN @DropDown3 IS NOT NULL 
                THEN N' OR ColumnName3 IN (SELECT Val FROM dbo.split(@DropDown3)) '
                    ELSE N'' END

  EXECUTE sp_executesql @SQL
                       ,N'@DropDown1 VARCHAR(1000), @DropDown2 VARCHAR(1000), @DropDown3 VARCHAR(1000)'
                       ,@DropDown1 
                       ,@DropDown2 
                       ,@DropDown3 
END 

答案 1 :(得分:0)

我会把它放在一个存储过程中,@ star1,2等会是位(1/0)变量..

SELECT * FROM Table_P tp inner join Table_D td ON tp.id= td.id where
(
 (td.star_rating =1 and @star1 = 1) 
or 
 (td.star_rating =2 and @star2 = 1) 
or 
 (td.star_rating =3 and @star3 = 1) 
or 
 (td.star_rating =4 and @star4 = 1) 
or 
 (td.star_rating =5 and @star5 = 1) 
)