我正在尝试使用带有两个文本框的AjaxToolKit CalendarExtender控件来开发搜索过滤器。第一个TextBox用于(startdate),第二个TextBox用于完成日期,单击(搜索)按钮后,结果应显示在ListView中,并在ListView的第一列添加一个复选框。
仅供参考,数据库设计如下:
Employee Table: Username, Name...
SuggestionsLog: ID, Title, Description, DateSubmitted, StatusID
SuggestionsStatus: ID, Status
(DateSubmitted是一个datetime数据类型字段)
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();
}
答案 0 :(得分: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)
答案 1 :(得分:1)
BETWEEN假设当天开始的午夜:
因此它将有效排除 @finishDate。如果您使用'Between'运算符,请注意这一点。如果这不是您想要的,请在建立时间窗口时使用'<', '>', '<=', '>='
运算符。
WHERE (NOT((finishDate <= @startDate) OR (startDate > @finishDate)))