惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
Schneier on Security
博客园 - 【当耐特】
人人都是产品经理
人人都是产品经理
大猫的无限游戏
大猫的无限游戏
Last Week in AI
Last Week in AI
IT之家
IT之家
V
V2EX
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
博客园 - 叶小钗
Martin Fowler
Martin Fowler
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
M
MIT News - Artificial intelligence
博客园 - 聂微东
MyScale Blog
MyScale Blog
云风的 BLOG
云风的 BLOG
The Cloudflare Blog
WordPress大学
WordPress大学
Microsoft Azure Blog
Microsoft Azure Blog
阮一峰的网络日志
阮一峰的网络日志
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - 三生石上(FineUI控件)
Cyberwarzone
Cyberwarzone
Scott Helme
Scott Helme
H
Help Net Security
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
The GitHub Blog
The GitHub Blog
Cisco Talos Blog
Cisco Talos Blog
D
DataBreaches.Net
P
Palo Alto Networks Blog
T
Threatpost
P
Privacy & Cybersecurity Law Blog
L
LINUX DO - 最新话题
Know Your Adversary
Know Your Adversary
C
CXSECURITY Database RSS Feed - CXSecurity.com
B
Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
P
Proofpoint News Feed
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Cloudbric
Cloudbric
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
N
News and Events Feed by Topic
T
The Blog of Author Tim Ferriss
L
Lohrmann on Cybersecurity
量子位
Security Latest
Security Latest
Google DeepMind News
Google DeepMind News
Y
Y Combinator Blog
aimingoo的专栏
aimingoo的专栏
Forbes - Security
Forbes - Security
T
Tailwind CSS Blog

博客园 - Franz

从如此简单的代码谈起 谈谈C#基元类型 TFS代码签入指导 并发之阿喀琉斯之踵 too many automatic redirections were attempted 预览Cube出现没有注册类错误 Transaction Manager Maximum Timeout .NET下的延迟加载 我的VisualStudio工具箱 简单的谈一下.NET下的AOP TFS上使用Beyond Compare来比较源码 SqlBulkCopy 是个好对象 释放Sql Server内存 吐槽一下Silverlight的SaveFileDialog. 书评《模式-工程化实现及扩展》 定义加载动画 代码共享的小技巧 WPF将控件保存为图片 《编程人生》的书评
SmallDateTime时间范围检查
Franz · 2014-04-22 · via 博客园 - Franz

2014-04-22 14:50  Franz  阅读(639)  评论()    收藏  举报

SamllDataTime是SQL Server中的一种表达时间的类型, 精度不高但是省空间,具体的细节就查看一下MSDN的文档吧. http://msdn.microsoft.com/zh-cn/library/ms182418.aspx

这里着重看一下它的时间范围从1900-01-01 到 2079-06-06.

首先展示一段系统中的代码, 你能发现其中的问题么?

SqlCommand command = new SqlCommand(sqlTemplate, conn);
//....
command.Parameters.Add(new SqlParameter("@LogClientTime", SqlDbType.SmallDate;
command.Parameters.Add(new SqlParameter("@LogServerTime", SqlDbType.SmallDate;
//....
foreach (var hourlySummaryData in _summaryData)
{
    //....
    command.Parameters["@LogClientTime"].Value = 
    hourlySummaryData.LogClientTime < SqlDateTime.MinValue ? SqlDateTime.MinValue : hourlySumma.Value.LogClientTime;
    command.Parameters["@LogServerTime"].Value = 
    hourlySummaryData.LogServerTime < SqlDateTime.MinValue ? SqlDateTime.MinValue : hourlySumma.Value.LogServerTime;
    //....
    command.ExecuteNonQuery();
}

系统中的时间未检查hourlySummaryData.LogClientTime的上限是不, 的确是个bug. 但是这不是我想说,这里还有一个bug.

SqlDateTime.MinValue的值是1753/1/1 0:00:00. 知道问题出在哪里了吧. 

.NET BCL中并没有任何一个公共静态字段时间是1900/1/1 (私有的还是有的, 比如SqlDateTime中的SQLBaseDate字段.)

所以使用的时候一定要特别的注意, 如果数据库中使用了SmallDateTime那么范围检查一定要自己手写一下. SqlDateTime.MinValue跟MaxValue不是给SmallDateTime用的, 是datetime的范围检查(1753 年 1 月 1 日到 9999 年 12 月 31 日).