我有一个Registration
带有名称和日期时间的 SQL Server 表,我想用小于的日期时间搜索数据datetime.now
。
SqlDataAdapter cs =
new SqlDataAdapter("Select * from Registration where Date > DateTime.now")
我希望日期时间比今天少。
我有一个Registration
带有名称和日期时间的 SQL Server 表,我想用小于的日期时间搜索数据datetime.now
。
SqlDataAdapter cs =
new SqlDataAdapter("Select * from Registration where Date > DateTime.now")
我希望日期时间比今天少。
T-SQL 内置函数getdate()
返回当前服务器日期/时间。
因此,您的查询应该是SELECT * FROM Registration WHERE [Date] < getdate()
.
在您的代码中使用它是:
SqlDataAdapter cs = new SqlDataAdapter("Select * from Registration where [Date] < getdate()")
您可以使用 SQLGETDATE()
来执行此操作:
SqlDataAdapter cs = new SqlDataAdapter("Select * from Registration where Date < GETDATE()")
使用GETDATE()获取查询中的当前日期。
SqlDataAdapter cs = new SqlDataAdapter("Select * from Registration where Date < GETDATE()");
这可能有效......如果您想使用.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 输出以匹配。
它非常简单..
如果您想使用 sql 当前日期时间,请使用 SELECT * FROM Registration WHERE [Date] < getdate()
如果您希望您的应用程序当前日期时间使用
"SELECT * FROM Registration WHERE [Date] < '+Datetime.Now.Tostring() +'"
如果您想要第二个选项,也尝试使用参数化查询。