数据字符串不比较

时间:2013-12-11 04:39:11

标签: c#

我正在尝试将c#中的日期字符串与Access DB 2010中的日期字符串进行比较。不知怎的,我的查询不通过,我不知道我哪里出错了。代码如下:

string dateNow = DateTime.Now.ToString("dd/MM/yyyy");

string dbcommand = "SELECT log.logID, log.datetime, log.startfloor, log.destination, log.status FROM log WHERE (((log.datetime) Like '" + dateNow + "'));";

在数据库中,记录如下:

1  25/11/2013 10:18:11
2  25/11/2013 12:15:22
3  09/12/2013 09:12:46

3 个答案:

答案 0 :(得分:1)

string dateNow = DateTime.Now.ToString("dd/MM/yyyy");

string dbcommand = "SELECT log.logID, log.datetime, log.startfloor, log.destination, log.status FROM log WHERE format(log.datetime,'dd/MM/yyyy') = '" + dateNow + "'";

答案 1 :(得分:0)

试试这个

string dateNow=DateTime.Now.ToString("dd-MM-yyyy HH:mm:ss")

由于您的数据库中的行包含上述格式,您还需要比较h,m和s

答案 2 :(得分:0)

然后使它像

where log.datetime between '2013/11/25 00:00:00' and '2013/11/25 23:59:59' 

string dateNow = DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss");
string dbcommand = ".... where log.datetime between dateNow + ' 00:00:00' and dateNow  + ' 23:59:59' 

自己看看