DateTime issue in SQL Server

后端 未结 5 837
我在风中等你
我在风中等你 2021-01-18 22:08

I\'m trying to do an INSERT into an already set DB in MS SQL Server, the DB server is in a shared hosting (godaddy).

And what i\'m trying to achieve is to store an a

5条回答
  •  [愿得一人]
    2021-01-18 22:22

    Be aware that MS SQL Server's DateTime and the C# DateTime have different MinValues.

    While MS SQL DateTime begins only at {1/1/1753 12:00:00 AM} the C# DateTime start with {1/1/0001 12:00:00 AM}.

    There are 3 approaches that come to my mind:

    1. Make sure in your code that the object that you want to save has a valid DateTime value set before saving it. The other posters already made some suggestions for this.

    2. Add a default value to the column on the database side. To me, this seems to be the easiest and cleanest approach. You would neither need to change the data type nor would it affect your code and you would make sure that no invalid date would (or could) be saved.

    3. If this is possible at your stage of development, you may change the data type of the column to DateTime2, which allows for dates beginning with {1/1/0001 12:00:00 AM}. MSDN documentation here.

提交回复
热议问题