DateTime issue in SQL Server

后端 未结 5 834
我在风中等你
我在风中等你 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.

    0 讨论(0)
  • 2021-01-18 22:25

    try DateTime.Now and if its case of textfeild DateTime.Now.ToString(); hope this work.

    It works fine in almost same case i have.

    1/1/0001 12:00:00 AM etc are defaults of sqlServer and in above comment if after deleting the method its doing nothing means that its not updating the reference or something like this.

    Make sure what you want to delete is deleted and if its not then enforce it by changing the properties and refresh + check

    0 讨论(0)
  • 2021-01-18 22:25

    I cant understand why you are converting a datetime into String ??

     datetime.Text = DateTime.Now.ToString("yyyyMMddHHmmss");
    

    if you have to insert datetime in your DB then just pass that datetime variable to insert command.

    DateTime dt = new DateTime();
    dt = DateTime.Now.Date;
    

    When you will insert it to DB, DB will convert it to its appropriate datetime format. Just try it.

    0 讨论(0)
  • 2021-01-18 22:32

    According to me,it would be as follow:

    datetime.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
    
    0 讨论(0)
  • 2021-01-18 22:39

    I would assume that Servicios has DateTime property which is not being set.

    DateTimes are not nullable, and when they're not set, they default to {1/1/0001 12:00:00 AM}.

    Upon calling db.SubmitChanges(), you're attempting to insert a value into a DateTime column which is a lower value that SQL SERVER supports as seen in your exception.

    SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM.

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