The conversion of a varchar data type to a datetime data type resulted in an out-of-range value

时间:2022-02-22 15:40:36

刚刚有在程序中,传递一个空值至MS SQL Server数据库,这个值的数据类型为DATETIME
执行时,它却发生了如标题提示的异常:
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.

跟踪一下,当遇上一个空值时,它并没有真正是给一个空值给数据库,而是Datetime的最小值"1/1/0001 12:00:00 AM"
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value

 

在两个文本框都是空值时,跟踪如下:
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value

 

当这个值传入数居库时,数据库转换这个字符值为DateTime出错了:
试执行一下下面的SQL语句:

The conversion of a varchar data type to a datetime data type resulted in an out-of-range value

经查资料,是因为DateTime数据类型的最小值为"1/1/1753 12:00:00 AM",如果想把"1/1/0001 12:00:00 AM"正确转换为日期时间,那得需要使用DATETIME2数据类型。
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value