Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# DateTime.TryParseExact不返回值in out参数?_C#_Mysql_Datetime - Fatal编程技术网

C# DateTime.TryParseExact不返回值in out参数?

C# DateTime.TryParseExact不返回值in out参数?,c#,mysql,datetime,C#,Mysql,Datetime,下面提到的问题已经解决,代码已经更改以反映更改 我想对照MySQL数据库中存储的日期检查当前日期。为了检索当前日期,我正在使用nist时间服务器,该服务器以MM-DD-YYYY格式返回日期,在我的数据库中,我的值以“YYYY-MM-DD”格式存储。我要做的是检查我传递的日期是否等于数据库中的日期,然后做些什么 我使用的代码如下所示: // The code originally posted has been changed to reflect changes made and it is n

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

我想对照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命令,但找不到任何命令。

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

顺便说一下,我想您需要一个格式字符串“yyyy-MM-dd”

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


实际上,您也不应该将
x
转换为字符串-它已经是
DateTime
,那么为什么要将其转换为字符串,然后对其进行解析呢?您的评论表明,您认为一个
DateTime
值有一种特定的格式,它与数字不同,格式与值如何转换为文本有关,并且不是值本身固有的。

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

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);

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

跳过所有日期时间到字符串和返回转换,并使用参数。您应该始终使用参数,因为将参数转换为字符串是一项繁重的工作,而且容易受到攻击

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

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

请注意,
DateTime currentDate=InternetTime()
可能应替换为
DateTime currentDate=InternetTime().Date
,假设OP试图使用
ToSortDateString()
获取
DateTime
对象的日期部分。我使用了您的解决方案,它成功了。但有一件事应该是MySqlCommand cmd=newmysqlcommand(“选择id,从open_dates中选择日期作为oDates,其中dates=?currentDate”,connection);谢谢,我不知道ToString能做到。非常感谢..你从几行无意义的代码中保存了我的程序。交换是为了测试目的。我花了很多时间试图修复代码,但没有完成清理。我知道,我需要处理一些糟糕的编码实践:D再次感谢您,非常感谢您提供的参数化查询功能。我之前没有意识到这种脆弱性。我曾经认为它们只是两种不同的写作风格:(,愚蠢,我知道。我将编辑我的问题,以反映我所做的改变。感谢你们所有人
da.SelectCommand.Parameters["?currentDate"] = currentDate;