How to get a date from MySql database to C# DateTime object?

前端 未结 3 1746
暗喜
暗喜 2021-01-23 01:20

I have a MySQL database with a certain field that contains a date.

How can I convert it to a C# DateTime object?

And how do I convert a C# Dat

相关标签:
3条回答
  • 2021-01-23 01:55

    We use MySQL and C# at my shop for a number of applications. The solution that I used for anything to do with dates was to use BIGINT values within the DB and store all my DateTime objects there as UTC values. For my needs this solution worked really well and was straightforward. This avoided any problems between converting to/from different date formats. The DB side is oblivious to the fact that I'm storing dates and the C# clients could easily convert anything from the Int64 to the DateTime.

    Anyhow, that was my solution and it worked for me.

    Best of luck!

    0 讨论(0)
  • 2021-01-23 01:58

    in database mysql yyyy-mm-dd

    so

    string date = DateTime.Now.ToString("yyyy-MM-dd");
    
    0 讨论(0)
  • 2021-01-23 02:01

    You'll need the MySQL Connector for .NET, if you don't already have it, in order to get your C# application talking to MySQL.

    Then you'll be able to get the date columns out of your database and into DateTime objects in the standard way, using MySqlConnection, MySqlCommand, MySqlDataReader etc.

    There are a few potential gotchas when converting between MySQL dates/times and .NET DateTimes, but there's a useful section in the MySQL documentation with advice on how to handle the issues.

    0 讨论(0)
提交回复
热议问题