MySql HasRows总是返回true
本文关键字:返回 true HasRows MySql | 更新日期: 2023-09-27 18:30:46
我的代码有问题:
using (MySqlConnection conn = new MySqlConnection(cnstr))
{
conn.Open();
MySqlCommand checkuser = new MySqlCommand("select * from table1 where UserID='A00001' and FHDate < '" + DateTime.Now + "'",conn);
MySqlDataReader chk = checkuser.ExecuteReader();
if (chk.HasRows)
{ ....... }
当 FHDate='2016-01-17' chk 时。HasRows返回true是这个正确答案,但 FHDate='2016-01-19' chk。HasRows仍然返回true。我能找到我做错的地方吗?
请帮忙。
我觉得您的FHDate
列是一些日期时间类型的,但您提供DateTime
值作为string
,它仍然返回 true 进行字符串比较。
将此DateTime.Now
值与参数化查询一起添加,并提供其参数类型,MySqlDbType
作为日期时间,如果除此之外一切都是 okkey,它应该是 oky。
MySqlCommand checkuser = new MySqlCommand("select * from table1 where UserID='A00001' and FHDate < @date", conn);
checkuser.Parameters.Add("@date", MySqlDbType.Datetime).Value = DateTime.Now;
...
还可以使用 using
语句来释放命令和读取器,就像对连接执行的操作一样。