开发者

DateTime.TryParseExact doesnt return value in out parameter?

The problem mentioned below has been solved and code has been changed to reflect changes.

I want to check the current date against a date stored in my MySQL database. to retrieve the current date I am using the nist time server which returns a date in the format MM-DD-YYYY and in my database I have values stored in the format "YYYY-MM-DD." What I want to do is checking if the date I pass is equal to the date in the database, then do something.

The code I am usi开发者_C百科ng is the following one:

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

the "dt" table remains empty because TryParseExact doesn't write anything to dateValue and so it remains "01-01-0001" or something like that. Where am I going wrong? It would be great if anyone could help me. There is actually a workaround that I thought of; I could store strings in the database right away, and then check them, but that would be cheating; I want the date to work. I tried searching for MySQL commands that could help me, but I couldn't find any.


You're ignoring the result of DateTime.TryParseExact. It will almost certainly be returning false, indicating that it's failed to parse the string - and it's documented to write DateTime.MinValue into the out parameter in that case. You should definitely be checking the return value and acting accordingly if parsing is failing.

I suspect you want a format string of "yyyy-MM-dd" by the way.

Note that you shouldn't be including the date in your SQL string directly in the following line - you should use a parameterized query instead.

Indeed, you shouldn't be converting x to a string either - it's already a DateTime, so why are you converting it to a string and then parsing it? Your comment indicates that you think a DateTime value has a particular format - it doesn't, any more than numbers do - formats relate to how a value is converted into text, and isn't inherent in the value itself.


Skip all the DateTime to string and back transformation and use parameters. You should always use parameters since converting a parameter to string is heavy work and you're prone to SQL injection attacks.

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

You can also prepare the SqlCommand/DataAdapter once and change the parameter's value every time you need to:

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


First of all, why are you doing all these swappings. It would be easier to say:

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

Second, your format YYYY-MM-DD is not valid. It should be yyyy-MM-dd as specified in ToString().

Third, if you really want to store your date as a string then format it in the SQL statement that you are generating by using

MySqlDataAdapter da = new MySqlDataAdapter("SELECT id,dates AS oDates FROM open_dates     WHERE dates=DATE("+x.ToString("yyyy-MM-dd")+");", con);

This way you can ignore all your processing logic defined previously.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜