Losing DateTimeOffset precision when using C#

Posted by Darvis Lombardo on Stack Overflow See other posts from Stack Overflow or by Darvis Lombardo
Published on 2010-03-18T14:42:27Z Indexed on 2010/03/18 14:51 UTC
Read the original article Hit count: 257

I have a SQL Server table with a CreatedDate field of type DateTimeOffset(2).
A sample value which is in the table is 2010-03-01 15:18:58.57 -05:00

As an example, from within C# I retrieve this value like so:

var cmd = new SqlCommand("SELECT CreatedDate FROM Entities WHERE EntityID = 2", cn);  
var da = new SqlDataAdapter(cmd);  
DataTable dt =new DataTable();  
da.Fill(dt);

And I look at the value:
MessageBox.Show(dt.Rows[0][0].ToString());

The result is 2010-03-01 15:18:58 -05:00, which is missing the .57 that is stored in the database.

If I look at dt.Rows[0][0] in the Watch window, I also do not see the .57, so it appears it has been truncated.

Can someone shed some light on this? I need to use the date to match up with other records in the database and the .57 is needed.

Thanks!
Darvis

© Stack Overflow or respective owner

Related posts about sql-server-2008

Related posts about datetimeoffset