0

我有一个Registration带有名称和日期时间的 SQL Server 表,我想用小于的日期时间搜索数据datetime.now

SqlDataAdapter cs = 
    new SqlDataAdapter("Select * from Registration where Date > DateTime.now")

我希望日期时间比今天少。

4

5 回答 5

4

T-SQL 内置函数getdate()返回当前服务器日期/时间。

因此,您的查询应该是SELECT * FROM Registration WHERE [Date] < getdate().

在您的代码中使用它是:

SqlDataAdapter cs = new SqlDataAdapter("Select * from Registration where [Date] < getdate()")
于 2013-02-22T15:56:57.503 回答
3

您可以使用 SQLGETDATE()来执行此操作:

SqlDataAdapter cs = new SqlDataAdapter("Select * from Registration where Date < GETDATE()")
于 2013-02-22T15:56:55.927 回答
2

使用GETDATE()获取查询中的当前日期。

SqlDataAdapter cs = new SqlDataAdapter("Select * from Registration where Date < GETDATE()");
于 2013-02-22T15:58:27.687 回答
1

这可能有效......如果您想使用.NET DateTime,那将是本地系统的日期。

     SqlDataAdapter cs = new SqlDataAdapter(
           "SELECT * FROM [Registration] 
            WHERE [Date] < '" + DateTime.Now.ToShortDateString() +"';");

     //output: SELECT * FROM [Registration] WHERE [Date] < '2/22/2013';

它将取决于 [date] 列的数据类型,如果它是字符串或日期时间或其他东西。如果是 YYYYmmdd,那么您可以操作 DateTime.Now 输出以匹配。

于 2013-02-22T16:16:18.277 回答
1

它非常简单..

如果您想使用 sql 当前日期时间,请使用 SELECT * FROM Registration WHERE [Date] < getdate()

如果您希望您的应用程序当前日期时间使用

"SELECT * FROM Registration WHERE [Date] < '+Datetime.Now.Tostring() +'"

如果您想要第二个选项,也尝试使用参数化查询。

于 2013-02-22T16:21:48.470 回答