【问题标题】:DateTime.TryParseExact doesnt return value in out parameter?DateTime.TryParseExact 不在 out 参数中返回值?
【发布时间】:2011-10-24 20:44:53
【问题描述】:

下面提到的问题已经解决,代码已经改变以反映变化。

我想根据存储在我的 MySQL 数据库中的日期检查当前日期。要检索当前日期,我使用的是 nist 时间服务器,它返回格式为 MM-DD-YYYY 的日期,在我的数据库中,我有以“YYYY-MM-DD”格式存储的值。我要做的是检查我通过的日期是否等于数据库中的日期,然后做一些事情。

我使用的代码如下:

// The code originally posted has been changed to reflect changes made and it is now working fine (problem solved) a big thank you to all those who replied.
DateTime currentDate = InternetTime();
//DD/MM/YYYY is returned here convert to YYYY/MM/DD
string currentDate = x.ToString("yyyy-MM-dd");
con = new MySqlConnection(conString);
MySqlDataAdapter da = new MySqlDataAdapter();
MySqlCommand cmd = new MySqlCommand("SELECT id AS oDates FROM open_dates WHERE dates=?currentDate",con);
cmd.Parameters.AddWithValue("?currentDate",currentDate);
da.SelectCommand = cmd;
DataTable dt = new DataTable("openDates");
da.Fill(dt);

“dt”表仍然是空的,因为 TryParseExact 没有向 dateValue 写入任何内容,因此它仍然是“01-01-0001”或类似的东西。我哪里错了?如果有人可以帮助我,那就太好了。 实际上,我想到了一种解决方法;我可以立即将字符串存储在数据库中,然后检查它们,但那是作弊;我希望日期有效。 我尝试搜索对我有帮助的 MySQL 命令,但找不到。

【问题讨论】:

    标签: c# mysql datetime


    【解决方案1】:

    您忽略了DateTime.TryParseExact 的结果。它几乎肯定会返回false,表明它无法解析字符串 - 并且在这种情况下记录了将DateTime.MinValue 写入out 参数。您应该绝对检查返回值并在解析失败时采取相应措施。

    怀疑顺便说一句,你想要一个格式字符串“yyyy-MM-dd”。

    请注意,您不应该将日期直接包含在 SQL 字符串的下一行中 - 您应该改用参数化查询。

    确实,您也不应该将x 转换为字符串——它已经是DateTime,那么为什么要将其转换为字符串然后解析呢?您的评论表明您认为 DateTime 值具有特定格式 - 它没有,与数字一样 - 格式与值如何转换为 text 相关,并且不是固有的在值本身中。

    【讨论】:

    • 非常感谢您提供参数化查询。我之前没有意识到这个漏洞。我曾经认为它们只是 2 种不同的写作风格 :(,愚蠢,我知道。我将编辑我的问题以反映我所做的更改。感谢你们所有人
    【解决方案2】:

    跳过所有 DateTime 到字符串和反向转换并使用 参数。您应该始终使用参数,因为将参数转换为字符串是一项繁重的工作,而且您很容易受到SQL injection 攻击。

    DateTime currentDate = InternetTime().Date;
    
    MySqlDataAdapter da = new MySqlDataAdapter();
    
    MySqlCommand cmd = new MySqlCommand(
        "SELECT id, dates AS oDates FROM open_dates WHERE dates=?currentDate", connection);
    cmd.Parameters.Add("?currentDate", MySqlDbTypes.DateTime, currentDate);
    
    da.SelectCommand = cmd;
    
    DataTable dt = new DataTable("openDates");
    da.Fill(dt);
    

    您也可以准备一次 SqlCommand/DataAdapter 并在每次需要时更改参数的值:

    da.SelectCommand.Parameters["?currentDate"] = currentDate;
    

    【讨论】:

    • 请注意,DateTime currentDate = InternetTime(); 可能应该替换为 DateTime currentDate = InternetTime().Date;,假设 OP 正在尝试使用 ToShortDateString() 来获取 DateTime 对象的 Date 部分。
    • 我使用了您的解决方案,它奏效了。一件事虽然应该是 MySqlCommand cmd = new MySqlCommand("SELECT id, dates AS oDates FROM open_dates WHERE dates=?currentDate",connection);谢谢
    【解决方案3】:

    首先,您为什么要进行所有这些交换。这会更容易说:

    string currentDate = x.ToString("yyyy-MM-dd"); // This gives you the format that you need
    

    其次,您的格式 YYYY-MM-DD 无效。它应该是 ToString() 中指定的 yyyy-MM-dd。

    第三,如果您真的想将日期存储为字符串,请在您生成的 SQL 语句中使用

    对其进行格式化
    MySqlDataAdapter da = new MySqlDataAdapter("SELECT id,dates AS oDates FROM open_dates     WHERE dates=DATE("+x.ToString("yyyy-MM-dd")+");", con);
    

    这样您可以忽略之前定义的所有处理逻辑。

    【讨论】:

    • 我不知道 ToString 可以做到这一点。非常感谢......你从几行无意义的代码中拯救了我的程序。交换是出于测试目的。我花了很多时间试图修复代码,但清理工作没有完成。我需要处理一些不好的编码实践,我知道 :D 再次感谢
    猜你喜欢
    • 1970-01-01
    • 2020-05-12
    • 2021-11-19
    • 2019-12-25
    • 1970-01-01
    • 2013-01-25
    • 2013-06-28
    • 2011-02-20
    • 1970-01-01
    相关资源
    最近更新 更多