C# MySql总是返回true

C# MySql总是返回true,c#,mysql,asp.net,C#,Mysql,Asp.net,我的代码中有一个问题: 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

我的代码中有一个问题:

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)
    {  .......  }
使用(MySqlConnection conn=newmysqlconnection(cnstr))
{
conn.Open();
MySqlCommand checkuser=new MySqlCommand(“从表1中选择*,其中UserID='A00001'和FHDate<'”+DateTime.Now+“”,conn);
MySqlDataReader chk=checkuser.ExecuteReader();
if(chk.HasRows)
{  .......  }
当FHDate='2016-01-17'chk.HasRows return true为正确答案时, 但是FHDate='2016-01-19'chk.HasRows仍然返回true。我可以找到哪里做错了


请帮助。

我觉得您的
FHDate
列是键入的某个日期时间,但您将
Datetime
值作为
字符串提供,对于字符串比较,它仍然返回true

添加这个
DateTime。现在
值加上一个,并将它的参数类型加上
MySqlDbType
作为DateTime,如果除此之外一切正常,那么它应该是正常的

MySqlCommand checkuser = new MySqlCommand("select * from table1 where UserID='A00001' and FHDate < @date", conn);
checkuser.Parameters.Add("@date", MySqlDbType.Datetime).Value = DateTime.Now;
...
MySqlCommand checkuser=new MySqlCommand(“从表1中选择*,其中UserID='A00001'和FHDate<@date”,conn);
checkuser.Parameters.Add(“@date”,MySqlDbType.Datetime).Value=Datetime.Now;
...

还可以使用
使用
语句像处理连接一样处理命令和读取器。

我觉得您的
FHDate
列是某种日期时间类型,但您将
日期时间
值作为
字符串
提供,对于字符串比较,它仍然返回true

添加这个
DateTime。现在
值加上一个,并将它的参数类型加上
MySqlDbType
作为DateTime,如果除此之外一切正常,那么它应该是正常的

MySqlCommand checkuser = new MySqlCommand("select * from table1 where UserID='A00001' and FHDate < @date", conn);
checkuser.Parameters.Add("@date", MySqlDbType.Datetime).Value = DateTime.Now;
...
MySqlCommand checkuser=new MySqlCommand(“从表1中选择*,其中UserID='A00001'和FHDate<@date”,conn);
checkuser.Parameters.Add(“@date”,MySqlDbType.Datetime).Value=Datetime.Now;
...

还可以使用
使用
语句像处理连接一样处理命令和读取器。

该代码迫切需要SQL注入…@cfrozendath:那么你是说chk.HasRows总是返回true的原因是由于SQL注入的可能性吗???尝试将查询更改为从表1中选择*,其中UserID='A00001'和FHDateFHDate
)是
true
例如,您是如何提供此
2016-01-19
的?它像
DateTime.Date.AddDays(1)
还是什么?我认为您的逻辑有点错误。我将代码修改为:MySqlCommand checkuser=new MySqlCommand(“从fahuiuser中选择FHDate,其中UserID='A00001'和FHDate<@FHDate order by FHDate desc limit 1”,conn);检查user.Parameters.AddWithValue(“@FHDate”,DateTime.Now);它仍然返回true@Leec.c.您是否在数据库管理器中执行过此查询?其中没有返回任何行?@Leec.c.您所说的正确结果是什么意思?“没有数据”“你的意思是?'2016-01-17'<'2016-01-18'将列出数据,;2016-01-19'@Leec.c.你不是在WHERE子句中比较你的值,而是在条件(即
FHDate
)为
true
的情况下过滤你的结果,例如,你是如何提供这个
2016-01-19
的?是否像
DateTime.Date.AddDays(1)
还是什么?我认为你的逻辑有点错误。