【发布时间】:2015-03-23 17:39:28
【问题描述】:
我知道SQL中这个日期范围之间存在记录,但我尝试了各种格式,甚至将范围扩大了很多,但仍然没有返回结果。我在这个查询中做错了什么导致没有记录被返回?感谢您的帮助。
DateTime today = DateTime.Now;
DateTime tomorrow = today.AddDays(1);
DateTime yesterday = today.AddDays(-1);
DateTime dt = DateTime.Now;
string yesterdayString = yesterday.ToString("yyyy-MM-dd HH:mm:ss");
string tomorrowString = tomorrow.ToString("yyyy-MM-dd HH:mm:ss");
int itemNum = 0;
string query = "select DocID, CNo, DocType, DocNumber from errordata where cno = '" + cno + "' and createddate > '" + yesterdayString + "' and createddate < '" + tomorrowString + "' order by '" + sorton + "'desc";
【问题讨论】:
-
SQL Injection alert - 您应该不将您的 SQL 语句连接在一起 - 使用 参数化查询 来避免 SQL 注入
-
因为您应该在查询中使用参数
-
也许您没有得到结果,因为您也在过滤“CNO”并且该陈述不正确?
-
你不是因为无效的 sql 而得到异常吗?
-
很抱歉,我删除了一些代码,包括正在选择的几行。我不小心删除了 from 部分,但是,查询本身仍然包含 from 。我已经尝试添加参数,但它仍然没有返回任何记录。我在 SQL 中测试了相同的查询并且它有效,这只是我假设的 C# 日期的问题。