在 C# 中从 SQLServer 查询日期?

发布于 2025-01-07 19:28:19 字数 727 浏览 0 评论 0原文

我正在构建一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谜兔 2025-01-14 19:28:19

只需使用大于和小于

using (HumanResourcesDB db = newHumanResourcesDB(ConfigurationManager
            .ConnectionStrings["HumanResourcesDB"].ConnectionString))
{
    List<Employee> record = (from tab in db.Employees
     where tab.Birthday >= DateTime.Today
     && tab.Birthday < DateTime.Today.AddDays(31)
     select tab).ToList();

    this.grdBirthdays.DataSource = record;

    this.grdBirthdays.DataBind();
}

另外,我应该提到我使用了 DateTime.Today 而不是 DateTime.Now,因为今天表示一天的开始为 00:00:00.000。如果某人的生日设置为今天的 00:00:00.000,并且您使用了 DateTime.Now(假设是早上 8:00)。生日不会包含在这个范围内,因为它被认为是在“现在”之前。

Just use a greater than and less than

using (HumanResourcesDB db = newHumanResourcesDB(ConfigurationManager
            .ConnectionStrings["HumanResourcesDB"].ConnectionString))
{
    List<Employee> record = (from tab in db.Employees
     where tab.Birthday >= DateTime.Today
     && tab.Birthday < DateTime.Today.AddDays(31)
     select tab).ToList();

    this.grdBirthdays.DataSource = record;

    this.grdBirthdays.DataBind();
}

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".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文