On my application i assign a DateTime.Now to a new DateTime. then i assign it to a value and write it to the database. But then when i get it back from the database and compare the variable to the new datetime assigned earlier it differs with a couple of milliseconds.
Anyone has an idea why this happens?
E.g.
DateTime var1 = DateTime.Now;
Object1.DateTime = var1;
Database.SaveObject = var1
Object2 = Database.FindObjectById(objectId);
Assert.AreEqual(var1, Object2.DateTime);
This is most likely an issue with the precision of DB’s
datetimecolumn: it does not have enough accuracy to store the time up to a millisecond. When you usedatetime, the time portion is rounded to increments of .000, .003, or .007 seconds.Switching column’s type to
datetime2should help, because its resolution is 100 nanoseconds.