【问题标题】:How to display the search results based on two dates in the ListView?如何在 ListView 中显示基于两个日期的搜索结果?
【发布时间】:2012-07-08 10:30:08
【问题描述】:

我正在尝试使用带有两个文本框的 AjaxToolKit CalendarExtender 控件来开发搜索过滤器。第一个文本框用于(开始日期),第二个文本框用于结束日期,单击(搜索)按钮后,结果应显示在 ListView 中,并在 ListView 的第一列添加一个复选框。

仅供参考,数据库设计如下:

Employee Table: Username, Name...
SuggestionsLog: ID, Title, Description, DateSubmitted, StatusID
SuggestionsStatus: ID, Status

(DateSubmitted 是一个日期时间数据类型字段)

StartDate 和 EndDate 将基于数据库中 SuggestionsLog 表中的 DateSubmitted 列。 问题是从 Ajax CalendarExtender 中选择的日期格式为(2012 年 7 月 2 日)。 另外,我编写了以下查询:

SELECT     dbo.SafetySuggestionsLog.Title, dbo.SafetySuggestionsLog.Description, dbo.SafetySuggestionsLog.Username, dbo.SafetySuggestionsLog.DateSubmitted, 
                      dbo.SafetySuggestionsStatus.Status
FROM         dbo.SafetySuggestionsLog INNER JOIN
                      dbo.SafetySuggestionsStatus ON dbo.SafetySuggestionsLog.StatusID = dbo.SafetySuggestionsStatus.ID
WHERE     (dbo.SafetySuggestionsLog.DateSubmitted = @startDate) AND (dbo.SafetySuggestionsLog.DateSubmitted = @finishDate)

我想在 ListView 中显示结果,但是 ListView 上没有显示任何内容,我不知道为什么。 您能帮我解决这个问题吗?

ASP.NET:

<div>
                    <asp:Label ID="Label2" runat="server" Text="From: " />
                    <asp:TextBox ID="txtStartDate" runat="server" />
                    <asp:CalendarExtender ID="CalendarExtender1" runat="server" Enabled="True" 
                        Format="MMMM d, yyyy" TargetControlID="txtStartDate">
                    </asp:CalendarExtender>

                    <asp:Label ID="Label3" runat="server" Text="To: " />
                    <asp:TextBox ID="txtEndDate" runat="server" /> 
                    <asp:CalendarExtender ID="CalendarExtender2" runat="server" Enabled="True" 
                        Format="MMMM d, yyyy" TargetControlID="txtEndDate">
                    </asp:CalendarExtender>
                    <asp:Button ID="searchButton" runat="server" Text="Search" 
                        onclick="searchButton_Click" />

                    <asp:ListView ID="FilteredSuggestions" runat="server"></asp:ListView>
                </div>

代码隐藏:

protected void searchButton_Click(object sender, EventArgs e)
    {
        string connString = ConfigurationManager.ConnectionStrings["testConnectionString"].ConnectionString;
        SqlConnection conn = new SqlConnection(connString);
        string cmd = @"SELECT     dbo.SafetySuggestionsLog.Title, dbo.SafetySuggestionsLog.Description, dbo.SafetySuggestionsLog.Username, dbo.SafetySuggestionsLog.DateSubmitted, 
                                 dbo.SafetySuggestionsStatus.Status
                        FROM         dbo.SafetySuggestionsLog INNER JOIN
                                    dbo.SafetySuggestionsStatus ON dbo.SafetySuggestionsLog.StatusID = dbo.SafetySuggestionsStatus.ID
                        WHERE     (dbo.SafetySuggestionsLog.DateSubmitted = @startDate) AND (dbo.SafetySuggestionsLog.DateSubmitted = @finishDate)";

        SqlDataAdapter sda = new SqlDataAdapter(cmd,conn);
        sda.SelectCommand.Parameters.AddWithValue("@startDate", txtStartDate.Text);
        sda.SelectCommand.Parameters.AddWithValue("@finishDate", txtEndDate.Text);
        DataSet ds = new DataSet();
        sda.Fill(ds, "table");

        FilteredSuggestions.DataSource = ds.Tables["table"];
        FilteredSuggestions.DataBind();
    }

【问题讨论】:

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


    【解决方案1】:

    试试这个:

    使用 BETWEEN 指定日期范围

    SELECT     dbo.SafetySuggestionsLog.Title, dbo.SafetySuggestionsLog.Description, dbo.SafetySuggestionsLog.Username, dbo.SafetySuggestionsLog.DateSubmitted, 
                          dbo.SafetySuggestionsStatus.Status
    FROM         dbo.SafetySuggestionsLog INNER JOIN
                          dbo.SafetySuggestionsStatus ON dbo.SafetySuggestionsLog.StatusID = dbo.SafetySuggestionsStatus.ID
    WHERE     (dbo.SafetySuggestionsLog.DateSubmitted Between @startDate AND @finishDate)
    

    【讨论】:

    • 感谢您的帮助。我真的很感激。
    【解决方案2】:

    BETWEEN 假定一天开始时的午夜:

    T-SQL Between Dates Confusion

    所以它会有效地排除@finishDate。如果您使用的是“Between”运算符,请注意这一点。如果这不是您想要的,请在建立时间窗口时使用 '&lt;', '&gt;', '&lt;=', '&gt;=' 运算符。

    WHERE (NOT((finishDate <= @startDate) OR (startDate > @finishDate)))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-05
      • 2020-07-08
      • 2016-12-11
      相关资源
      最近更新 更多