简体   繁体   English

从SQL中比较C#和GetDate()的DateTime.now的准确性

[英]Accuracy of comparing DateTime.now of C# and GetDate() from SQL

What i am doing is that, i need to select a row that i have just recently added through DateTime to get the PK since i need it. 我正在做的是,我需要选择一个我刚刚通过DateTime添加的行来获取PK,因为我需要它。

I store the DateTime through: 我通过以下方式存储DateTime:

DateTime nw = DateTime.now and i use nw to search through my table. DateTime nw = DateTime.now ,我用nw搜索我的表格。

My question is that, what if let's say i put 2 rows within a span of 1 minute? 我的问题是,如果让我说我在1分钟的范围内放了2行怎么办? My sql table stores them like this: 我的sql表存储它们像这样:

在此输入图像描述

Since milliseconds isn't visible, will both of them be selected?(assuming everything happened within 1 minute) 由于毫秒不可见,它们都会被选中吗?(假设一切都在1分钟内发生)

Edit: this is from my asp mvc project. 编辑:这是我的asp mvc项目。 So the DateTime is new everytime my action is run. 因此,每次运行我的操作时, DateTime都是新的。

The problem is precision. 问题是精确度。 The GetDate() function in TSQL is not at the precision as c# DateTime, as GetDate() returns an TSQL DateTime. TSQL中的GetDate()函数的精度不是c#DateTime,因为GetDate()返回TSQL DateTime。

TSQL DateTime : TSQL日期时间

Defines a date that is combined with a time of day with fractional seconds that is based on a 24-hour clock. 定义一个日期,该日期与基于24小时制的小数秒的时间相结合。

Rounded to increments of .000, .003, or .007 seconds 舍入为.000,.003或.007秒的增量

C# DateTime : C#DateTime

The Ticks property expresses date and time values in units of one ten-millionth of a second, and the Millisecond property returns the thousandths of a second in a date and time value. Ticks属性以百万分之一秒为单位表示日期和时间值,Millisecond属性在日期和时间值中返回千分之一秒。 However, if you are using repeated calls to the DateTime.Now property to measure elapsed time, and you are concerned with small time intervals less than 100 milliseconds, you should note that values returned by the DateTime.Now property are dependent on the system clock, which on Windows 7 and Windows 8 systems has a resolution of approximately 15 milliseconds. 但是,如果您使用重复调用DateTime.Now属性来测量已用时间,并且您关心的是小于100毫秒的小时间间隔,则应注意DateTime.Now属性返回的值取决于系统时钟在Windows 7和Windows 8系统上,分辨率约为15毫秒。

However you could use the newer (avail as of SQL Server 2008) SysDateTime() which returns a datetime2(7) value that should match the precision of C# Datetime. 但是,您可以使用较新的(从SQL Server 2008开始) SysDateTime() ,它返回一个datetime2(7)值,该值应该与C#Datetime的精度相匹配。

datetime2(7) : datetime2(7)

Defines a date that is combined with a time of day that is based on 24-hour clock. 定义与基于24小时制的时间相结合的日期。 datetime2 can be considered as an extension of the existing datetime type that has a larger date range, a larger default fractional precision, and optional user-specified precision. datetime2可以视为现有日期时间类型的扩展,它具有更大的日期范围,更大的默认小数精度和可选的用户指定精度。

This only academically interesting because you should never use a datetime as a PK . 这只有学术上的有趣,因为你永远不应该使用日期时间作为PK

Let's say it's Nov 6, 2016 at 1:15AM. 我们假设它是2016年11月6日上午1:15。 You create a record: 您创建一条记录:

MyPk
------
2016-11-06 01:15:00

One hour later you create another record... 一小时后你创建另一条记录......

MyPk
------
2016-11-06 01:15:00
2016-11-06 01:15:00

Duplicate PKs due to daylight savings. 由于夏令时,重复PK。 Don't have daylight savings? 没有夏令时? There are a multitude of reasons to not use DateTime for a PK (simply google search for datetime as primary key ). 有很多理由不使用DateTime作为PK(只需谷歌搜索datetime作为主键 )。

Just to name a few: 仅举几个:

  • Exact select can be very difficult (milliseconds matter!) 精确选择可能非常困难(毫秒很重要!)
  • Foreign Keys become a Nightmare 外键成为梦魇
  • Replication is very difficult unless all systems are in the same timezone 除非所有系统都在同一时区,否则复制非常困难

If you really want to use the DateTime.Now with second precision as a way to find the PK of your data, you should not declared it once and use it everywhere. 如果您确实希望使用具有second精度的DateTime.Now作为查找数据PK的方法,则不应将其声明为一次并在任何地方使用它。 Rather, you should use it like this: 相反,你应该像这样使用它:

insertDataToDataBase(data, DateTime.Now);

and then 10-20 seconds later 然后10-20秒后

insertDataToDataBase(data, DateTime.Now); //still use DateTime.Now

This way your DateTime.Now will always be updated 这样,您的DateTime.Now将始终更新

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM