Azure 上SQL Database(PaaS)Time Zone时区问题处理(进阶篇)
通常ISV在面对本地客户时对时间相关的处理,一般都时区信息都是不敏感的。但是现在云的世界里为了让大家把时间处理的方式统一起来,云上的服务都是以UTC时间为准的,现在如果作为一个ISV来说就算你面对的客户只是本地用户但是你打算利用云来为你进行的应用提供更多的功能和便捷性时,你就需要采用UTC时间来处理跟相关的代码了。
在SQL Server数据库处理时间相关的数据时,我们常常会使用DateTime类型或者DateTime2类型来处理数据,其实早在SQL Server 2008发布时,数据库就开始支持DatetimeOffset数据类型了,DatetimeOffset天生出来就是为了处理时区问题的

参考上一个Blog 《Azure 上SQL Database(PaaS)Time Zone时区问题处理》,我们同样要来解决时区的问题,我们创建appcount表时,我们采用下面语句
CREATE TABLE [dbo].[appcount2](
[Id] [int] IDENTITY(1,1) NOT NULL,
[createtime] [datetime] NULL,
[CreatetimeWithOffset] [Datetimeoffset] NULL,
PRIMARY KEY CLUSTERED (Id)
)
插入数据时使用
INSERT INTO appcount(createtime,createtimewithoffset) VALUES(GetDate(),sysdatetimeoffset() AT TIME ZONE 'China Standard Time')
插入完数据我们将数据在查询出来


我们会发现DateTimeOffset在处理时区问题时不管带不带时区的迁移语句的出来的时间都是正确的,但是DateTime字段的出来的时间明显就会有时间偏移错误。
我们用下面的C#从.NET程序里面将这些时间数据取出来会如何呢?
using (IDbConnection conn = new System.Data.SqlClient.SqlConnection(strConn))
{
conn.Open();
var command = conn.CreateCommand();
command.CommandText = "Select id,createtime,CreatetimeWithOffset from appcount";
var reader = command.ExecuteReader();
while (reader.Read())
{
var id = reader["id"];
var date = reader["createtime"];
var date2 = reader["createtimeWithOffset"];
var localDate = ((DateTimeOffset)date2).LocalDateTime;
Console.WriteLine("id:{0},createtime:{1},DateTimeInOffSet:{2},localdate:{3}", id, date,date2, localDate);
}
reader.Close();
conn.Close();
}
这里是代码执行结果:

小结:
采用DatetimeOffset来存储时间,通过DatetimeOffset来处理时间可以让你的代码更加稳健,更加国际范
相关文章:《Azure 上SQL Database(PaaS)Time Zone时区问题处理》
Azure 上SQL Database(PaaS)Time Zone时区问题处理(进阶篇)的更多相关文章
- Azure 上SQL Database(PaaS)Time Zone时区问题处理
在Azure上面的PaaS时间都是以UTC时间为准(云的世界里基本上都是以UTC时间为标准的),所以以前在本地SQL Server上面常用的GetDate()方法会碰到问题,在中国获取的时间会被当前时 ...
- [Windows Azure] Managing SQL Database using SQL Server Management Studio
Managing Windows Azure SQL Database using SQL Server Management Studio You can use Windows Azure SQL ...
- [Windows Azure] Windows Azure Storage & SQL Database
http://channel9.msdn.com/Series/Windows-Azure-Storage-SQL-Database-Tutorials Windows Azure offers mu ...
- [Windows Azure] Monitoring SQL Database Using Dynamic Management Views
Monitoring Windows Azure SQL Database Using Dynamic Management Views 5 out of 7 rated this helpful - ...
- 在Windows Azure上配置VM主备切换(1)——Linux篇
对任何一个上线系统来说,高可用设计是不可或缺的一个环节,这样才可以确保应用可以持续.稳定的运行,而不是频繁的掉线.停机.高可用设计的核心思路很简单,就是消除一切单点故障,将单点链路或者节点升级为多点. ...
- Java连接Azure SQL Database
Azure SQL Database是Azure上的数据库PAAS服务,让用户可以快速的创建和使用SQL数据库而不用担心底层的备份,安全,运维,恢复等繁琐的工作,本文简单介绍如何使用Java程序连接到 ...
- [Windows Azure] Guidelines for Connecting to Windows Azure SQL Database
Guidelines for Connecting to Windows Azure SQL Database 6 out of 12 rated this helpful - Rate this t ...
- [Windows Azure] Learn SQL Reporting on Windows Azure (9-Step Tutorial)
Learn SQL Reporting on Windows Azure (9-Step Tutorial) 4 out of 4 rated this helpful - Rate this top ...
- Azure SQL Database (21) 将整张表都迁移到Azure Stretch Database里
<Windows Azure Platform 系列文章目录> Azure SQL Database (19) Stretch Database 概览 Azure SQL Da ...
随机推荐
- jaxb教程(忘记了过来看看)
链接 原文链接
- BugPhobia开发篇章:Scurm Meeting-更新至0x02
0x01 :目录与摘要 If you weeped for the missing sunset, you would miss all the shining stars 索引 提纲 整理与更新记录 ...
- 结对&团队之1715|K班取经
声明:同学请勿抄袭,追责莫要怪我: 因为暂时闲着没事,就翻阅学长学姐的博客找找灵感,个人觉得应该还有人和我一样对软工实践未来的一大段路还很天真,包括目前的结对作业和团队组队也很迷路,于是写下这篇博客提 ...
- testng对执行失败的用例,再次执行
前段时间在网络上看到通过重写TestNG的接口,可以再次执行失败的测试用例,于是学习了,我之前的做法是当自己的脚本中碰到异常,就自动调用方法本身来达到再次执行用例的目的,这个过程中有设定重试的次数 对 ...
- UDJC用户自定义Java类
private RowSet t1 = null;//业务表步骤 private RowSet t2 = null;//删除步骤 public boolean processRow(StepMetaI ...
- redis批量删除key 命令
redis-cli -n 数据库编号 -a 密码 keys "过滤条件" | xargs redis-cli -n 数据库编号 -a 密码 del Demo: redis-cli ...
- Qt Lighthouse学习(二),就是QPA(Qt Platform Abstraction) 项目的名字
上一次关注Qt Lighthouse是在6月初,可是现在都8月底了.时间真快... Lighthouse 是 QPA(Qt Platform Abstraction) 项目的名字,它使得将Qt移植到新 ...
- dividend = Integer.parseInt(args[0])参数问题
先来一段代码: package yichang; public class MyExceptionTest { public static void main(String[] args) { int ...
- 理解 Delphi 的类(十一) - 深入类中的方法[8] - 抽象方法与抽象类
//抽象方法类似与接口; 在没有接口的年代 Delphi 是用抽象方法来模拟接口的; 我想它最终会被接口替代. {下面就定义了两个抽象方法} TMyClass = class(TObject) p ...
- ROC曲线和PR曲线绘制【转】
TPR=TP/P :真正率:判断对的正样本占所有正样本的比例. Precision=TP/(TP+FP) :判断对的正样本占判断出来的所有正样本的比例 FPR=FP/N :负正率:判断错的负样本占所 ...