构建一个select查询作为gridview数据源asp.net

时间:2014-04-04 18:06:42

标签: asp.net datasource

我使用asp.net gridview依赖于控件来搜索 日: - 月: - 年:

在数据库表中我得到一个startdate字段包含整个日期:

我尝试这个查询:

SELECT        id, shift_id, name_of_shift, person_in_shift, starttime_in_shift, endtime_in_shift, table_id, startdate, enddate, point_id
FROM            sarcshifttable
WHERE         (id IN
                             (SELECT        MIN(id) AS Expr1
                               FROM            sarcshifttable AS sarcshifttable_1
                               GROUP BY table_id)) AND (DATEPART(year, startdate) + '' LIKE @year) AND (DATEPART(day, startdate) + '' LIKE @day) AND (DATEPART(month, startdate) + '' LIKE @month)

控件:

    <asp:TextBox ID="day_search" CssClass="textfield NjmeDine_Integer" Width="40px" runat="server"></asp:TextBox>

<asp:TextBox ID="month_search" CssClass="textfield NjmeDine_Integer" Width="40px" runat="server"></asp:TextBox>

<asp:TextBox ID="year_search" CssClass="textfield NjmeDine_Integer" Width="80px" runat="server"></asp:TextBox>

选择参数:

<SelectParameters>
                <asp:ControlParameter ControlID="year_search" DefaultValue="%" Name="year" PropertyName="Text" />
                <asp:ControlParameter ControlID="month_search" DefaultValue="%" Name="month" PropertyName="Text" />
                <asp:ControlParameter ControlID="day_search" DefaultValue="%" Name="day" PropertyName="Text" />
            </SelectParameters>

此格式的开始日期:2014-03-01

注意: 当我在数据库中使用静态值进行尝试时,它可以完美地工作

SELECT        id, shift_id, name_of_shift, person_in_shift, starttime_in_shift, endtime_in_shift, table_id, startdate, enddate, point_id
FROM            sarcshifttable
WHERE        (DATEPART(month, startdate) + '' LIKE 03) AND (id IN
                             (SELECT        MIN(id) AS Expr1
                               FROM            sarcshifttable AS sarcshifttable_1
                               GROUP BY table_id)) AND (DATEPART(year, startdate) + '' LIKE 2014) AND (DATEPART(day, startdate) + '' LIKE 01) AND (DATEPART(month, startdate) + '' LIKE 03)

1 个答案:

答案 0 :(得分:0)

答案是:

    SELECT        id, shift_id, name_of_shift, person_in_shift, starttime_in_shift, endtime_in_shift, 

SELECT        id, shift_id, name_of_shift, person_in_shift, starttime_in_shift, endtime_in_shift, table_id, startdate, enddate, point_id
FROM            sarcshifttable
WHERE        (DATEPART(month, startdate) + '' LIKE 03) AND (id IN
                             (SELECT        MIN(id) AS Expr1
                               FROM            sarcshifttable AS sarcshifttable_1
                               GROUP BY table_id)) AND ((DATEPART(year, startdate) + '' LIKE '%'+@year+'%') AND (DATEPART(day, startdate) + '' LIKE '%'+@day+'%') AND (DATEPART(month, startdate) + '' LIKE '%'+@month+'%')