通常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时区问题处理(进阶篇)的更多相关文章

  1. Azure 上SQL Database(PaaS)Time Zone时区问题处理

    在Azure上面的PaaS时间都是以UTC时间为准(云的世界里基本上都是以UTC时间为标准的),所以以前在本地SQL Server上面常用的GetDate()方法会碰到问题,在中国获取的时间会被当前时 ...

  2. [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 ...

  3. [Windows Azure] Windows Azure Storage & SQL Database

    http://channel9.msdn.com/Series/Windows-Azure-Storage-SQL-Database-Tutorials Windows Azure offers mu ...

  4. [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 - ...

  5. 在Windows Azure上配置VM主备切换(1)——Linux篇

    对任何一个上线系统来说,高可用设计是不可或缺的一个环节,这样才可以确保应用可以持续.稳定的运行,而不是频繁的掉线.停机.高可用设计的核心思路很简单,就是消除一切单点故障,将单点链路或者节点升级为多点. ...

  6. Java连接Azure SQL Database

    Azure SQL Database是Azure上的数据库PAAS服务,让用户可以快速的创建和使用SQL数据库而不用担心底层的备份,安全,运维,恢复等繁琐的工作,本文简单介绍如何使用Java程序连接到 ...

  7. [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 ...

  8. [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 ...

  9. Azure SQL Database (21) 将整张表都迁移到Azure Stretch Database里

    <Windows Azure Platform 系列文章目录>  Azure SQL Database (19) Stretch Database 概览      Azure SQL Da ...

随机推荐

  1. kafka handler

    1.配置kafka 参数文件 在ogg主目录下有示例文件: [root@WH0PRDBRP00AP0013 ogg]# cd AdapterExamples/big-data/kafka/ [root ...

  2. 利用Hibernate子查询(in) 得到部分字段(实体类的构造函数)

    感人= = 终于弄好了 String hql="select new Shop(s.strid,s.shopname,s.tradearea,s.discountinfo,s.beginti ...

  3. mongo导入导出命令

    1.导出工具:mongoexport     1.概念:         mongoDB中的mongoexport工具可以把一个collection导出成JSON格式或CSV格式的文件.可以通过参数指 ...

  4. 第二章 Socket用法详解

    构造Socket Socket构造方法如下: Socket() //Creates an unconnected socket, with the system-default type of Soc ...

  5. 关于react 官方脚手架使用出现的问题

    首先按照官网说明,一路的安装下来,很顺利,然后开始运行吧,提示个错误,缺少index.js 文件  原来是默认给出的文件是App.js 如何更改呢 找到react-script模块文件夹config下 ...

  6. Scrum项目6.0 和8910章读后感

    Scrum项目6.0总结 这次sprint1通过我们的努力,终于把自动回复做出来了.但之后的任务更加繁重,我们要更加努力,克服各种困难. 也要说说这次自动回复里面的注意之处: 1.不该空格的地方空格, ...

  7. 利用css制作带边框的小三角

    标签(空格分隔):css 在项目中会使用到的小实例,目前知道的有两种方法来实现 设置元素的宽和高,利用rotate实现,比较简单的一种 div{ width: 10px; height: 10px; ...

  8. [转帖]DAS、SAN、NAS

    http://blog.itpub.net/26736162/viewspace-2214368/ DAS(Direct-attached Storage) 直连存储 直连式存储与服务器主机之间的连接 ...

  9. 拦截器的顺序是按照xml中的顺序执行的

  10. BZOJ 3173 最长上升子序列(树状数组+二分+线段树)

    给定一个序列,初始为空.现在我们将1到N的数字插入到序列中,每次将一个数字插入到一个特定的位置.每插入一个数字,我们都想知道此时最长上升子序列长度是多少? 由于序列是顺序插入的,所以当前插入的数字对之 ...