1

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.

I store the DateTime through:

DateTime nw = DateTime.now and i use nw to search through my table.

My question is that, what if let's say i put 2 rows within a span of 1 minute? My sql table stores them like this:

enter image description here

Since milliseconds isn't visible, will both of them be selected?(assuming everything happened within 1 minute)

Edit: this is from my asp mvc project. So the DateTime is new everytime my action is run.

Carlos Miguel Colanta
  • 2,685
  • 3
  • 31
  • 49

2 Answers2

2

The problem is precision. The GetDate() function in TSQL is not at the precision as c# DateTime, as GetDate() returns an TSQL DateTime.

TSQL DateTime:

Defines a date that is combined with a time of day with fractional seconds that is based on a 24-hour clock.

Rounded to increments of .000, .003, or .007 seconds

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. 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.

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.

datetime2(7):

Defines a date that is combined with a time of day that is based on 24-hour clock. 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.

This only academically interesting because you should never use a datetime as a PK.

Let's say it's Nov 6, 2016 at 1:15AM. 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. 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).

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
Community
  • 1
  • 1
Erik Philips
  • 53,428
  • 11
  • 128
  • 150
1

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. Rather, you should use it like this:

insertDataToDataBase(data, DateTime.Now);

and then 10-20 seconds later

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

This way your DateTime.Now will always be updated

Ian
  • 30,182
  • 19
  • 69
  • 107
  • Using `DateTime.Now` in this fashion prevents testability. – Erik Philips Dec 30 '15 at 05:40
  • @ErikPhilips "prevents testability". I am sure that you are a lot more experienced on this topic and I am willing to learn. Do you mind to enlighten me? Thank you in advance! – Ian Dec 30 '15 at 07:37
  • [Unit Testing - DateTime.Now](http://stackoverflow.com/questions/2425721/unit-testing-datetime-now). – Erik Philips Dec 30 '15 at 19:46