将不确定变为确定~DateTime.MinValue和MaxValue引发的异常

回到目录

问题描述:

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

概念相关

.Net中的DateTime结构类中的MinValue和MaxValue有时会引发一个异常,那就是日期溢出的问题,您输入的日期必须介于 1753 年 1 月 1 日到 9999 年 12 月 31 日之间,而DateTime.MinValue实际上不是1753 年 1 月 1日,而是0000年1月1日,而数据库可以接收的日期时间最小值为1753/1/1,这时由于.net产生的DateTime.MinValue远远小于它,所以,它出现了日期溢出的问题。

解决问题

我们作一个测试:

   Console.Write(DateTime.MinValue);
Console.Write(DateTime.MaxValue);

它的结果为:

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMAAAAA2CAIAAAAedQFUAAAHZ0lEQVR4nO2a2U9UVxzHeRWxFQWr2Cq7KDgzIGAFFGzdyiKCobZ2SZvUpOLGpqDAsAqzsLXW2timGquNxtS0adOkCSS81TUpSf+G4Ulknbkj0NuH38zhzF3OnLvMgjnf/B7O/d7f/d1z7nw499x7ieB5/t9/HrJgoS4iEECRydUaY2VK9cqUUxBRqaeiUk9HpZ6O2nJ6lSfOrEo78xrE1rOvo9h2bjVE+rnV6eei02ui02uiM2qiM2rWZNSu2V67ZnvtWghD3VpDXYyhLsZYF2OsjzHWxxrrY031saaGWFPDOlPDukyI829keWJ91oX1OzyxIfvChuxGiLicxricpricprjcpo25TRtzL27MvfjmTm+8fektFLuaN0HkNW/Ka9kMkd8Sn98Sn98an98aX9CaUNCaUGBO2G1O2G1OhNjTlrinLWlPW1JhW1Jhe1Jhe3Jhe3JRe3JRR0pRR8peT6Tu7Ux9xxNb3u1CkbavK21fd9q+7q37IS5v3X952wGInm0He9JRHOrN8Mb29yyeKLYYii2GYquh2GoosRpLrMYSm7HUZiy1mUptplK7qcxuKrNnltkzD9szD/dlQZT3ZZX37yjv33EEYiD7yEB2hSdyKgchcisHc48OQew8OsQAYgDpClBHt4UyDlZdikw+yQBiAPkA9P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YiTkHKuvYp4xgAoRPCeH7KSMy6csViSdoIjLxBPuY+ip9ymD/zsEAYgAxgJYvQCxYqIglgNqZmJTLB6AiJiaFEgIUwcSkSAwgJk1iADFpEgOISZPEAI36CmUq9dFeRf0JXD65n/T5oaoTphIAJDlIvEHpS25KKtD56jqmbryBqxO+IgCE/3Go8+mvb4DyxQcK+kl26Merb53lJDmABL+QUh9Evi7ivfrmS2YS+inZcxXj1V5nOYkAUITUlEvpgwjXRXKXjvlymZL9pM8PVZ3wlSRAkuNU5IPkrkugfblMuX7S54eqTvhKDiDYKx4wpS+5STB1zCdkkvtJcBSNV3ud5aQgP4WRr5T2fMpMyh9evBmcOstJAXoPNCoS8iW7oVc+QXrlh6pOmIq9iWbSJAYQkyYxgJg0iQHEpEkMICZNYgAxaRIDiEmTyO+B8Ey9fLLEyUrrkPPpO6PLuEZFCm0d/eX33zn0bYslvhaCvYrqkPPpr7L2cZFzQlVHf4UWILRrFJNkAmUd8rH0l5jmvHK9xU11P7xedYKhEAIkd+HkukpfR1197edlABVFYL2XHIN2H99LY5J3Eeooqi8nRfXlDpfMJ1+fANXRX3JPYYp+MKV+QIsT9iq9xErrq6im4lfXq44+Cj5AhKHqQpu6U+tYX/vo9KoTDEkCFBJ6/ObLtWnyac6u9Lw0AKnrj151giHCGkiQqYsvN85Rkfz6ciMSJxPq0BQhlCIXIecrKqJjHZ3F3kQzaRIDiEmTGEBMWvQ/A997ULJTjEIAAAAASUVORK5CYII=" alt="" />

所以,如果你在程序中,进行数据库查询时,需要对日期进行查询,如果希望给日期变量一个默认值,那不会使用DateTime.MinValue,而要使用SQL的数据结果中的MinValue,它位于System.Data.SqlTypes名称空间下,表示与SQLSERVER对应的数据类型,测试代码如下:

 Console.WriteLine(System.Data.SqlTypes.SqlDateTime.MinValue);
Console.WriteLine(System.Data.SqlTypes.SqlDateTime.MaxValue);

它的结果为:

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAALQAAAAmCAIAAADiASYtAAACGUlEQVR4nO2aMXLDMAwE/TQ/zU9jmTJFHuROwwg4EABhQcncVgp5PALkTcby+PHz/fV6vZ7P54OQEwwHgTAcBMJwEAjDQSAyHGOMWTA01HF1ib+SkDikR3VG9V0+bZzCIctVG1OVUm83r9oaRPWqcrnQ2e9lPp0c4UBZjobDWKtOoX039WoNciEakbPX+NyL5X+OE2qTS/Fy3HaL6o2C/Zd60l/jcy9C4Zhnx29UpWpl6JebevRIOXyZRvoun0784UhMyfFQYhLjSCnvJqrv8unEGQ67B+cqw0SdiuoN5SFWSzJG5KV+1OdepMOBOvQ8R/09eqfSeanyz2t87oX6tuK8KlssT0EtAO0b1RtU6bt82uA3pATCcBAIw0EgDAeBMBwEwnAQCMNBIHM40Mt31biNFEd9bL2/mJK+hqDXJ8PpS7B519pniezzNBvysfX+E9zvy9Z0+WToCscxNSZUgdPHXus/Ps++qNp5MHepVT41tIQDHQoq0u+T89/f95+HY65MrW9/3OjEaM9/Qw/tcJf+iJA/Wq7q7fP5kE8G9QfG/suIjn/U3JiNHl/UP+GWuNEqHy9XhsNooyRJua0L/fe7q/KpYfPHPiXJWOrRs0fv2T26ryccuXqqfGpQP3MsK06Mox6GYDmOepFiw8djYljZJrY+ZFLoE4bfkBIIw0EgDAeBMBwEwnAQCMNBIAwHgeyEI/reT/4Wb/IjdMHJbYK1AAAAAElFTkSuQmCC" alt="" />

它在程序中的体现为:

     /// 用户列表
/// </summary>
/// <param name="username"></param>
/// <param name="deptname"></param>
/// <param name="page"></param>
/// <returns></returns>
public ActionResult Index(
string username,
string deptname,
DateTime? startTime,
DateTime? endTime,
int? page)
{ UserManager userManager = new UserManager();
ViewData["SiteMapID"] = ; var specification = new UserSpecification(startTime, endTime, username, deptname);
var model = userManager.GetWebManageUsers(specification, new PageParameters(page ?? , base.PageSize));
model.AddParameters = new System.Collections.Specialized.NameValueCollection();
model.AddParameters.Add("username", username);
model.AddParameters.Add("deptname", deptname);
model.AddParameters.Add("startTime", (startTime).ToString());
model.AddParameters.Add("endTime", (endTime).ToString()); if (Request.IsAjaxRequest())
return PartialView("List", model);
return View(model);
}

在上面的代码中,我们的DateTime类型,由于与数据库查询相关,所以,可以使用SqlDateTime类型代替!

回到目录

上一篇:Redis 数据结构的实现


下一篇:nginx跨域设置