小于15天的日期列的gridview [英] gridview for a date column less than 15 days old
问题描述
我有一个网格视图,其中有一列显示记录的发布日期
我希望网格视图显示过去15天内发布的数据
如何编写我的select命令,以便选择仅比当前日期和时间短15天的记录.
我正在将ASP.NET与C#一起使用
我在SqlSelectCommand中尝试了类似"WHERE DatePosted< = DateTime.UtcNow"的内容
并且还尝试了"WHERE DatePosted< = @datePosted"并添加了command.parameteres.AddwithValue("@ DatePosted",DateTime.UtcNow),但显然我缺少了一些重要的代码,因为它不起作用
我是ASP.NET的新手,迫切需要帮助来处理日期列
任何对新人有用的帮助都将受到高度赞赏
请帮助
最好的问候
I have a grid view with a column showing date on which record was posted
I want the grid view to show data which is posted within last 15 days
How do I write my select command so that the only records less than 15 days old compared to current date and time are selected.
I am using ASP.NET with C#
I have tried something like "WHERE DatePosted <= DateTime.UtcNow" in the SqlSelectCommand
and also tried "WHERE DatePosted <= @datePosted" and added command.parameteres.AddwithValue("@DatePosted", DateTime.UtcNow) but apparently I am missing some important code, as it does not work
I am new to ASP.NET and desperately need help to deal with date column
Any kind of help that can be useful for a new person shall be highly appreciated
Kindly help
Best Regards
推荐答案
尝试使用DATEADD()函数.您也可以使用负数.
DATEADD()函数教程 [
Try using the DATEADD() function. You can use negative numbers as well.
DATEADD() function tutorial[^]
可以在代码隐藏中提供动态命令.为了像我这样陷于这个问题的人们的利益,语法是
受保护的void nameofSqlDataSource_Selecting(对象发送者,SqlDataSourceSelectingEventArgs e)
{
e.Command.Parameters ["@ DateSent"].Value = DateTime.UtcNow.AddDays(-15);
}
您可以为几天提供任何数字,而不是15.您还可以参考任何控制参数动态定义任何日期.
A dynamic command can be given in codebehind. For benefit of those who are stuck at this problem like me, the syntax is
protected void nameofSqlDataSource_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
{
e.Command.Parameters["@DateSent"].Value = DateTime.UtcNow.AddDays(-15);
}
You can give any number for days in stead of 15. You can also define any date dynamically with reference to any control parameter.
这篇关于小于15天的日期列的gridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!