[英]Milliseconds value are not getting saved in SQL Server DateTime column
I need to store a DateTime value which is sent to the Database from a C# application which is using DateTime.UtcNow . 我需要存储一个DateTime值,该值从使用DateTime.UtcNow的C#应用程序发送到数据库。 If I save it to the DateTime column, the milliseconds value are always 000. But while debugging from the application, the milliseconds value is sent from the application to the database. 如果我将它保存到DateTime列,则毫秒值始终为000.但是在从应用程序进行调试时,毫秒值将从应用程序发送到数据库。 What am I missing? 我错过了什么?
It might happen because datetime
column rounds milliseconds part. 可能会发生这种情况,因为datetime
列会循环毫秒部分。 From documentation; 从文件;
Accuracy 准确性
Rounded to increments of
.000
,.003
, or.007
seconds 舍入为.000
,.003
或.007
秒的增量
Since you didn't show us how often you store your UtcNow
value (I assume you store it as a DateTime
, not it's string representation because parameterless ToString
and other standard formats does not generates millisecond part usually ), this may happen but if you do it in short time intervals, it would be really weird always rounding to 000
as milliseconds part. 因为你没有告诉我们如何往往你保存你UtcNow
值(你是我假设存储为DateTime
,而不是它的字符串表示,因为无参数ToString
等标准格式通常不会产生毫秒部分),这可能会发生,但如果你这样做它在很短的时间间隔内,总是很奇怪,总是以毫秒为单位舍入到000
。 But of course, we can't know that, yet. 但是,当然,我们还不知道。
On the other hand, datetime2
type does not do any rounding. 另一方面, datetime2
类型不进行任何舍入。 And it's accuracy is 100 nanoseconds. 它的准确度是100纳秒。
Accuracy 准确性
100 nanoseconds 100纳秒
Here's no pleasant way to use Datetime
because 这是使用Datetime
愉快方式,因为
SQL Server only stores time to approximately 1/300th of a second. SQL Server仅将时间存储到大约1/300秒。 These always fall on the 0, 3 and 7 milliseconds 这些总是落在0,3和7毫秒
SQL Server 2008 has much more precision available. SQL Server 2008具有更高的可用精度。 The datetime2
datatype will accurately store values like this: 2008-12-19 09:31:38.5670514
datetime2
数据类型将准确存储如下值: 2008-12-19 09:31:38.5670514
See reference documentation 请参阅参考文档
It looks to me like something/someone is doing a default-ToString instead of using ToString with the proper ISO-standard format. 它看起来像是某些东西/某人正在使用默认的ToString,而不是使用具有正确ISO标准格式的ToString。
Proper ISO-format for date with time is 适当的ISO格式的日期与时间是
yyyy-MM-ddTHH:mm:ss.fff
while date-only ISO-format is 而仅限日期的ISO格式是
yyyyMMdd
You can check: 你可以查看:
static void Main(string[] args)
{
System.DateTime cur = System.DateTime.UtcNow;
string strDefault = cur.ToString();
string str = cur.ToString("yyyy-MM-ddTHH:mm:ss.fff", System.Globalization.CultureInfo.InvariantCulture);
System.Console.WriteLine(str);
System.Console.WriteLine(strDefault);
}
This outputs 这输出
2016-03-03T08:31:27.324 2016-03-03T08:31:27.324
03.03.2016 08:31:27 03.03.2016 08:31:27
You might also want to use the HEX-format, as this enhances precision, or at least preserves a value that already was in SQL-Server. 您可能还希望使用HEX格式,因为这样可以提高精度,或者至少保留SQL-Server中已有的值。 If you don't use hex-representation, you can get a 23:59:59.997 value from SQL-server and resave, and you'll have 00:00:00 with day+1. 如果你不使用十六进制表示,你可以从SQL服务器获得一个23:59:59.997的值并重新保存,你将在00:00:00与day + 1。 Using the hex-format preserves .997, while saving an ISO-string will yield day+1. 使用十六进制格式保留.997,同时保存ISO字符串将产生第+ 1天。
public static string GetTimeAsHex(System.DateTime dt)
{
System.DateTime zero = new System.DateTime(1900, 1, 1);
System.TimeSpan ts = dt - zero;
System.TimeSpan ms = ts.Subtract(new System.TimeSpan(ts.Days, 0, 0, 0));
double x = System.Math.Floor(ms.TotalMilliseconds / 3.3333333333);
string hex = "0x" + ts.Days.ToString("X8") + System.Convert.ToInt32(x).ToString("X8");
return hex;
}
Anyway, as others have already told you, SQL-server datetime is only precise to within a 4 ms error margin. 无论如何,正如其他人已经告诉过你的那样,SQL服务器的日期时间只能精确到4毫秒的误差范围内。 This is why you should use datetime2, as it fixes many issues (bugs/"features") in datetime, including insufficient precision in the milliseconds range. 这就是为什么你应该使用datetime2,因为它修复了datetime中的许多问题(错误/“功能”),包括毫秒范围内的精度不足。
如果要将INSERT
语句构建为string
,请尝试在字符串转换中指定毫秒输出
DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss.fff", CultureInfo.InvariantCulture);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.