Convert DateTime for MySQL using C#

后端 未结 4 1471
悲&欢浪女
悲&欢浪女 2020-11-27 16:32

I want to change the DateTime for MySQL in C#.

My MySQL database only accept this format 1976-04-09 22:10:00.

In C# have a string who have a dat

4条回答
  •  难免孤独
    2020-11-27 17:03

    I would strongly suggest you use parameterized queries instead of sending values as strings in the first place.

    That way you only need to be able to convert your input format to DateTime or DateTimeOffset, and then you don't need to worry about the database format. This is not only simpler, but avoids SQL injection attacks (e.g. for string values) and is more robust in the face of database settings changes.

    For the original conversion to a DateTime, I suggest you use DateTime.ParseExact or DateTime.TryParseExact to explicitly specify the expected format.

提交回复
热议问题