在 C# 中从 SQLServer 查询日期?
我正在构建一个 Web 应用程序,它将显示存储在 SQL Server 2008 数据库中的人员列表即将到来的生日。我不知道如何查询数据库中距离今天日期 30 天内的所有记录。
这是我到目前为止所得到的:
using (HumanResourcesDB db = newH umanResourcesDB(ConfigurationManager.ConnectionStrings["HumanResourcesDB"].ConnectionString))
{
DateTime todaysDate = DateTime.Now;
List<Employee> record =
(from tab in db.Employees
where tab.Birthday between DateTime.Now and todaysDate.AddDays(30)
select tab).ToList();
this.grdBirthdays.DataSource = record;
this.grdBirthdays.DataBind();
}
当然,“之间”和“和”不起作用,这就是我需要填写的内容。我在网上搜索了一段时间,但没有结果。有什么帮助吗?
I am building a web application that is going to show the upcoming birthdays of a list of people stored in a SQL Server 2008 DB. I can't figure out how to query all records in the DB that are within 30 days of today's date.
Here's what I have so far:
using (HumanResourcesDB db = newH umanResourcesDB(ConfigurationManager.ConnectionStrings["HumanResourcesDB"].ConnectionString))
{
DateTime todaysDate = DateTime.Now;
List<Employee> record =
(from tab in db.Employees
where tab.Birthday between DateTime.Now and todaysDate.AddDays(30)
select tab).ToList();
this.grdBirthdays.DataSource = record;
this.grdBirthdays.DataBind();
}
of course the "between" and "and" don't work that's what I need filled in. I've searched the net for a little while to no avail. Any help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需使用大于和小于
另外,我应该提到我使用了 DateTime.Today 而不是 DateTime.Now,因为今天表示一天的开始为 00:00:00.000。如果某人的生日设置为今天的 00:00:00.000,并且您使用了 DateTime.Now(假设是早上 8:00)。生日不会包含在这个范围内,因为它被认为是在“现在”之前。
Just use a greater than and less than
Also I should mention that I have used DateTime.Today rather than DateTime.Now because today represents the start of the day as 00:00:00.000. If someones birthday is set to whatever today is at 00:00:00.000 and you have used DateTime.Now (let's assume it's 8:00 in the morning). There birthday will not be included in this range because it is considered before "now".