我使用 vb.net 和 ms access 2003 作为后端。当我使用如下所示的 sql 查询时,我的结果中没有收到任何记录。我正在尝试查找日期低于(或早于)当前日期的记录。
Dim Sql, CurrentDate as String
Dim TrDate as Date
Dim DataVal As Integer
TrDate=DateTime.Now
CurrentDate=Format(TrDate, "dd/MM/yyyy hh:mm:ss tt")
Sql="Select count(*) from Table1 where InvDate<=#" & CurrentDate & "#"
IF DBConOpen()=True
cmdOledb.CommandText = sql
cmdOledb.CommandType = CommandType.Text
cmdOledb.Connection = ConOledb
DataVal = cmdOledb.ExecuteScalar
msgbox(DataVal)
End IF
例如,如果我当前的日期是 01/09/2012 或 02/09/2012 或 .. 12/09/2012,数据库中的日期是 01/09/2012 或 02/09/2012 或 .. 12/09/2012。我应该得到相应的记录数。但在这种情况下我得到 0 条记录。
但是如果我当前的日期是 01/09/2012 或 02/09/2012 或 .. 12/09/2012 并且数据库中的日期是 20/08/2012 或 15/06/2012 或 .. 30/05/2012。我得到了正确的记录计数。
我在 MS Access 表中定义的日期格式是“常规日期”。有谁能发现问题所在吗?
最佳答案
您应该使用:
CurrentDate=Format(TrDate, "yyyy/MM/dd hh:mm:ss tt")
Access 需要明确的日期。
您可能还想删除时间元素。
关于ms-access - 使用 SQL 语句时使用 MS Access 数据库的 VB.Net 中的 DATETIME 格式问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12270816/