asp.net – 从SQL 2005 Server访问TimeZoneInfo
.NET TimeZoneInfo类非常棒,我认为它可以解决我在SQL 2005数据库中记录来自多个时区的数据的所有问题.
要将数据库中的UTC日期时间转换为任何其他时区,我只需使用TimeZoneInfo.FindSystemTimeZoneById()将时区转换为TimeZoneInfo类,然后调用TimeZoneInfo.ConvertTimeFromUtc().辉煌!我只是从SQL .NET CLR中调用它! 但是…… TimeZoneInfo的主机保护属性为MayLeakOnAbort. 当我使用VS 2008创建SQL函数或存储过程时,我甚至看不到system.TimeZoneInfo类永远不会使用它.我还假设即使我可以以某种方式引用TimeZoneInfo类,如果我尝试在SQL Sever 2005中注册程序集,我可能会得到某种安全性异常. 帮帮我!有没有办法从SQL Server 2005访问TimeZoneInfo类及其所有的财富? 注意:我刚刚在第一个答案后添加了这个警告: 我们在世界各地都有网站.我们需要在数据库中存储本地时间和UTC时间,以防止可能需要在站点级别进行趋势分析的事件.趋势可能包含一年超过52,000个数据点,因此,为了提高效率,我不能只在数据库中存储UTC时间并转换客户端上的每个数据点.因此,我需要能够在DB内将任何时区的本地时间转换为UTC时间和从UTC时间转换. 解决方法
我刚刚在SQL 2008数据库上完成了这个.
首先,我必须将数据库设置为值得信任并验证所有者是否正确. use [myDB] go alter database [myDB] set trustworthy on go exec sp_changedbowner 'sa' go 接下来,我创建了一个.NET解决方案 Imports System Imports System.Data Imports System.Data.SqlClient Imports System.Data.SqlTypes Imports Microsoft.SqlServer.Server Imports System.Collections.ObjectModel Imports System.Runtime.InteropServices Partial Public Class StoredProcedures <Microsoft.SqlServer.Server.SqlProcedure()> _ Public Shared Sub sp_ConvertTime(ByVal UTCTime As DateTime,ByVal ZoneID As String,<Out()> ByRef Output As DateTime) Dim sp As SqlPipe = SqlContext.Pipe Dim ConvertedTime As DateTime Dim tzUTC = TimeZoneInfo.FindSystemTimeZoneById("UTC") Dim tzNew = TimeZoneInfo.FindSystemTimeZoneById(ZoneID) ConvertedTime = TimeZoneInfo.ConvertTime(UTCTime,tzUTC,tzNew) Output = ConvertedTime sp.Send(ConvertedTime) ConvertedTime = Nothing tzUTC = Nothing tzNew = Nothing sp = Nothing End Sub End Class 在部署之前,我将权限级别设置为“不安全”. 接下来我部署了它,我检查了输出窗口中的构建错误并更正了这些错误. 这是SQL测试 DECLARE @UTCTime datetime DECLARE @ZoneID varchar(21) DECLARE @NewTime datetime SET @UTCTime = GETUTCDATE() SET @ZoneID = 'Central Standard Time' exec sp_ConvertTime @UTCTime,@ZoneID,@NewTime OUTPUT select @NewTime AS NewTime (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- 使用C#读取dbf行情文件
- asp.net-mvc – 在ASP.Net MVC中的同一视图中分隔两个表单
- asp.net – Web API服务 – 如何在异步任务中使用“HttpCon
- asp.net-mvc – 异步HttpModule MVC
- asp.net – 自定义Web字体在IE9中不起作用
- asp.net核心 – ASP – 启动时核心迁移EF核心SQL DB
- asp.net – 如何避免UpdatePanel在AutoPostBack上滚动?
- asp.net-mvc-4 – 为什么MVC4 @ Styles.Render()在调试模式
- 如何在VS 2010中停止调试时正常退出ASP.NET应用程序?
- 使用ASP.NET AJAX Control Toolkit设置焦点
- VS2017、VS2019没有Setup安装项目(Visual Studi
- asp.net – localhost页面无效. localhost重定向
- asp.net-mvc – Java是否与ASP.NET MVC的示例站点
- asp.net-mvc – 为什么visual studio 2012会在mv
- 快速开发之代码生成器(asp.net mvc4 + easyui +
- asp.net-mvc – 通过删除空格打破我的calc语句的
- ASP.NET MVC和FubuMVC之间的主要区别是什么?
- asp.net – 从承载令牌(Web API)获取自定义声明值
- asp.net – 如何设置sqldatasource参数的值?
- asp.net-core – 日期输入标记帮助程序未显示数据