SqlDateTime 溢出。必须介于 1/1/1753 12:00:00 AM 和 12/31/9999 11:59:59 PM之间---解决办法

传给数据库的时间类型的值为NULL,或者为DateTime.MinValue,或者为Convert.toDate(null),即(0001/01/01 0:00:00)

导致传到数据库的值为01年01月01日,从而导致溢出

using System;
using System.Collections.Generic;
using System.Data.SqlTypes;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            //.Net的最小时间和最大时间
            Console.WriteLine(DateTime.MinValue);
            Console.WriteLine(DateTime.MaxValue);

            //数据库的最小时间和最大时间
            Console.WriteLine(SqlDateTime.MinValue);
            Console.WriteLine(SqlDateTime.MaxValue);
        }
    }
}

调试输出结果


.NET中  DateTime最小值为: 0001-1-1 0:00:00

数据库中DateTime最小值为: 1753-1-1 0:00:00,

.NET中的最小值超出了Sql最小值的最小范围,导致数据溢出的错误。最大值一样

 

 

posted @ 2016-06-08 17:41  和田二星  阅读(948)  评论(0)    收藏  举报