0

我正在尝试使用带有两个文本框的 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();
    }
4

2 回答 2

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)
于 2012-07-08T10:34:36.597 回答
1

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

日期之间的 T-SQL 混淆

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

WHERE (NOT((finishDate <= @startDate) OR (startDate > @finishDate)))
于 2012-07-08T11:08:15.073 回答