.net - DateTimeOffset resolution in c# and SQL Server -
docs state in both .net , sql server resolution 100ns.
the time component of datetimeoffset value measured in 100-nanosecond units called ticks - c# accuracy - 100 nanoseconds - sql server
however sql seems drop last digit (e.g. i'm trying save 2013-08-15 09:19:07.2459675 -04:00, , sql saves 2013-08-15 09:19:07.2459670 -04:00 - notice last digit changes.)
this happens on same machine, not hardware dependent.
not need resolution, makes comparing dates harder.. , i'm curious.
i'll problem yours... little code show:
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 connection string specify server. // won't need initial catalog because // program uses temp table string connstr = "integrated security=true"; // temp table called #temp . cease exist @ end // of program automatically // 2 columns, datetimeoffset , shortdatetimeoffset string query = @"create table #temp (datetimeoffset datetimeoffset(7) not null, shortdatetimeoffset datetimeoffset(6) not null);insert #temp values (@dt1, @dt2);select * #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); // 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) , datetimeoffset(sql)"); } if (math.abs(dtrec2.ticks - dt.ticks) > 10) { throw new exception("too difference between datetimeoffset(.net) , 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); } } } } }
on 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
the "short" datetimeoffset(6)
.
Comments
Post a Comment