VB.NET 访问日期时间查询问题
VB.NET Access Datetime Querying Issue
我在 Access 数据库中有一堆记录 table 带有日期时间字段
例如记录 (2/2/2015 3:34:21 PM,2/2/2015 8:29:13 AM )
问题是我需要 运行 一个查询,其中我需要显示的所有记录都是发生在同一天的记录,而不考虑时间。如何最好地构建此查询?
我用了'Select * from table where thetime = 2/2/2015',没有返回结果。我将日期格式切换为以年份开头,运气不好。
有关 sql Access 查询语法的任何提示将不胜感激。谢谢
Access 中的 Date/Time 值始终包含日期和时间部分,因此像 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
我在 Access 数据库中有一堆记录 table 带有日期时间字段
例如记录 (2/2/2015 3:34:21 PM,2/2/2015 8:29:13 AM )
问题是我需要 运行 一个查询,其中我需要显示的所有记录都是发生在同一天的记录,而不考虑时间。如何最好地构建此查询?
我用了'Select * from table where thetime = 2/2/2015',没有返回结果。我将日期格式切换为以年份开头,运气不好。
有关 sql Access 查询语法的任何提示将不胜感激。谢谢
Date/Time 值始终包含日期和时间部分,因此像 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