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 ...
随机推荐
- oraclejdbc
https://segmentfault.com/q/1010000004952621/a-1020000004955600
- 【探路者】final贡献分配
[探路者]组成员及各位博客地址. 1蔺依铭:http://www.cnblogs.com/linym762/ 2张恩聚:http://www.cnblogs.com/zej87/ 3米赫:http: ...
- 调研Android的开发环境的发展演变
在 知道要做基于移动端的项目实践时,我就选定了Android,回来的时候查了很多相关的知识,很多人都在问开发安卓软件,使用eclipse还是用 Android studio?其实,也没有一个准确的答案 ...
- 程序开发入门工具之CodeBlocks
程序开发基础工具之CodeBlocks 作为程序开发工作者,我们会接触很多的程序开发软件:但实用以及容易掌握的程序开发软件对于初学者的学习能力是有一定的加成的.今天我就作为一个程序开发者给大家推荐一个 ...
- UTC时间与北京时间
经常混淆于此,特地研究了一下,记录在此以备忘. 整个地球分为二十四时区,每个时区都有自己的本地时间.在国际无线电通信场合,为了统一起见,使用一个统一的时间,称为通用协调时(UTC, Universal ...
- localStorage存储数组,对象,localStorage,sessionStorage存储数组对象
localStorage存储数组,对象,localStorage,sessionStorage存储数组对象 前言 最近在用angular做商城购物车的功能模块,因为angular的watch监听, ...
- TCP&UDP基础
TCP TCP/IP是一种网络通讯协议,而socket则是TCP/IP网络最为通用的API,即一种应用程序接口,称为套接字.TCP是面向连接的协议,在进行数据收发前必须连接,且在收发时必须保持该连接. ...
- SqlServer日期时间函数
-- 判断是否当天,createdate为日期字段 -- ╔════════════════════╗ -- ============================================= ...
- sqlserver中where条件加判断
我想实现如下功能: where case when (@a = null) then 1 = 1 else @a=a and b=@b 但是这样报错,经过翻阅资料找到如下解决方案: where (1 ...
- java 构造器二