c#和SQL Server中的DateTimeOffset分辨率
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c#和SQL Server中的DateTimeOffset分辨率,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2911字,纯文字阅读大概需要5分钟。
内容图文
Docs声明,在.NET和SQL服务器中,分辨率为100ns.
DateTimeOffset值的时间分量以100纳秒单位称为刻度 – C#
准确度 – 100纳秒 – SQL Server
然而SQL似乎丢掉了最后一位数(例如我正试图保存2013-08-15 09:19:07.2459675 -04:00,SQL保存2013-08-15 09:19:07.2459670 -04:00 – 通知最后一位数改变了.)
这发生在同一台机器上,因此它不依赖于硬件.
并不是说我实际上需要这个解决方案,但它使日期更难比较……我只是好奇.
解决方法:
我会说问题是你的…一个小代码显示:
namespace Test
{
using System;
using System.Data;
using System.Data.SqlClient;
using System.Globalization;
/// <summary>
///
/// </summary>
public class Program
{
/// <summary>
///
/// </summary>
public static void Main()
{
// Change the connection string to specify your server.
// Probably you won't need an initial catalog because this
// program uses a temp table
string connStr = "Integrated Security=True";
// The temp table is called #Temp . It will cease to exist at the end
// of the program automatically
// Two columns, DateTimeOffset and ShortDateTimeOffset
string query = @"CREATE TABLE #Temp (DateTimeOffset datetimeoffset(7) NOT NULL, ShortDateTimeOffset datetimeoffset(6) NOT NULL);INSERT INTO #Temp VALUES (@DT1, @DT2);SELECT * FROM #Temp";
using (var connection = new SqlConnection(connStr))
using (var command = new SqlCommand(query, connection))
{
const string dtString = "2013-08-15 09:19:07.2459675 -04:00";
const string dtFormat = "yyyy-MM-dd HH:mm:ss.fffffff zzz";
DateTimeOffset dt = DateTimeOffset.Parse(dtString, CultureInfo.InvariantCulture);
string dtString2 = dt.ToString(dtFormat, CultureInfo.InvariantCulture);
Console.WriteLine("Sending : {0}", dtString2);
// Just to be sure!
if (dtString != dtString2)
{
throw new Exception("Problem in conversion");
}
command.Parameters.Add("@DT1", SqlDbType.DateTimeOffset).Value = dt;
command.Parameters.Add("@DT2", SqlDbType.DateTimeOffset).Value = dt;
try
{
connection.Open();
using (SqlDataReader reader = command.ExecuteReader())
{
if (reader.Read())
{
DateTimeOffset dtRec1 = (DateTimeOffset)reader[0];
DateTimeOffset dtRec2 = (DateTimeOffset)reader[1];
string dtRecString1 = dtRec1.ToString(dtFormat, CultureInfo.InvariantCulture);
string dtRecString2 = dtRec2.ToString(dtFormat, CultureInfo.InvariantCulture);
Console.WriteLine("Receiving (long) : {0}", dtRecString1);
Console.WriteLine("Receiving (short): {0}", dtRecString2);
if (dtRec1 != dt)
{
throw new Exception("Difference between DateTimeOffset(.NET) and DateTimeOffset(sql)");
}
if (Math.Abs(dtRec2.Ticks - dt.Ticks) > 10)
{
throw new Exception("Too much difference between DateTimeOffset(.NET) and DateTimeOffset(6)(sql)");
}
if (reader.Read())
{
throw new Exception("Too many rows");
}
}
else
{
throw new Exception("No rows");
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
}
}
在我的SQL上:
Sending : 2013-08-15 09:19:07.2459675 -04:00
Receiving (long) : 2013-08-15 09:19:07.2459675 -04:00
Receiving (short): 2013-08-15 09:19:07.2459680 -04:00
“short”是DateTimeOffset(6).
内容总结
以上是互联网集市为您收集整理的c#和SQL Server中的DateTimeOffset分辨率全部内容,希望文章能够帮你解决c#和SQL Server中的DateTimeOffset分辨率所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。