SqlDateTime overflow / SqlDateTime 溢出

Error - SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM
SqlDateTime 溢出。必须介于 1/1/1753 12:00:00 AM 和 12/31/9999 11:59:59 PM之间

原因

使用C# insert或者update数据库时,datetime字段值为空默认插入C# 0001年01月01日 造成sql server datetime类型溢出。

C#中DateTime类型范围

DateTime.MinValue = 1/1/0001 12:00:00 AM
DateTime.MaxValue = 23:59:59.9999999, December 31, 9999, 
                    exactly one 100-nanosecond tick 
                    before 00:00:00, January 1, 10000

Sql Server中Datetime类型范围

between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM

解决办法

插入或更新DB时对DateTime类型字段进行判断,如果是null或者小于Sql server 最小值 1/1/1753 ,手动赋值 为 DateTime.MinValue

if(model.SubmitDateTime == null || model.SubmitDateTime == DateTime.MinValue)
{
	model.SubmitDateTime = new DateTime(1900, 1, 1);
}
posted on 2014-08-22 20:59  Frank.Fan  阅读(4193)  评论(0编辑  收藏  举报