.NET DateTime to SqlDateTime Conversion

前端 未结 6 1968
北恋
北恋 2020-12-01 15:49

While converting .NET DateTime (when is default(DateTime)) to SqlDateTime should I always check if the .NET date is between SqlDateTime.MinValue and SqlDateTime.MaxValue [or

6条回答
  •  情深已故
    2020-12-01 16:18

    Is it possible that the date could actually be outside that range? Does it come from user input? If the answer to either of these questions is yes, then you should always check - otherwise you're leaving your application prone to error.

    You can format your date for inclusion in an SQL statement rather easily:

    var sqlFormattedDate = myDateTime.Date.ToString("yyyy-MM-dd HH:mm:ss");
    

提交回复
热议问题