compare two datetime values from SQL Server with c#

后端 未结 9 2129
终归单人心
终归单人心 2021-01-05 20:48

i want to know how compare two datetime values one who is retreived from sql database and the other is the current one with c#

9条回答
  •  迷失自我
    2021-01-05 21:19

    System.Data.SqlTypes.SqlDateTime and System.DateTime use different underlying structures to represent dates.

    SqlDateTime represents the range January 1, 1753 to December 31, 9999 to an accuracy of 3.33 milliseconds

    DateTime(.NET Framework type) represents the range between Jan 1, 0001 to Dec,31 9999 to the accuracy of 100 nanoseconds

    You should be careful of these boundaries when comparing dates. One tactic to alleviate problems in comparisons could be to cast everything to System.DateTime and then perform the compare.

    You can use the Value property of the SqlDateTime struct (which returns a System.DateTime) to do the comparison elegantly and without explicit casting.

    You might find this article informative.

提交回复
热议问题