[英]VB.NET Access Datetime Querying Issue
我在带有日期时间字段的Access数据库表中有一堆记录
例如记录(2/2/2015 3:34:21 PM,2/2/2015 8:29:13 AM)
问题是我需要运行一个查询,其中需要显示的所有记录都是同一天发生的记录,而不管时间是什么。 如何最好地构造此查询?
我使用了“从表中选择*,其中time = 2/2/2015”,但没有返回结果。 我将日期格式切换为以年份开始,但是没有运气。
有关Access的sql查询语法的任何提示将不胜感激。 谢谢。
Access中的日期/时间值始终同时具有日期和时间部分,因此日期文字(如2015-02-02
)等效于2015-02-02 00:00:00
。 如果您想要该日期的所有行,而不考虑时间,则需要使用WHERE子句,例如
... WHERE thetime >= {that date} AND thetime < {the following day}
在VB.NET中执行此操作的正确方法是使用这样的参数化查询:
Using cmd As New OleDbCommand()
cmd.Connection = con ' an open OleDbConnection
cmd.CommandText =
"SELECT * FROM thetable " &
"WHERE thetime >= ? AND thetime < ?"
Dim targetDate As New DateTime(2015, 2, 2) ' example data
cmd.Parameters.Add("?", OleDbType.DBTimeStamp).Value = targetDate
cmd.Parameters.Add("?", OleDbType.DBTimeStamp).Value = targetDate.AddDays(1)
Using rdr As OleDbDataReader = cmd.ExecuteReader
Do While rdr.Read()
Console.WriteLine(rdr("thetime"))
Loop
End Using
End Using
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.