No milliseconds value when reading DateTime values from a SQL database in C?

Here's how I'd try to troubleshoot this: step through in the debugger and look at the type and value of readerTimestamp". If the type is not SqlDateTime, that would make me suspicious-- I'd then look at the query to figure out why that column was returning another type instead of DATETIME (or DATETIME2, etc. ) if that value is SqlDateTime and it does contains milliseconds, then I'd look at the cast as the source of the problem.To validate this, I'd try (in debugger or code) SqlDataReader.GetDateTime() and SqlDataReader.GetSqlDateTime() to see if either returns the correct result. This admittedly seems really unlikely as source of the problem-- casting should work fine if that value from #1 is SqlDateTime but contains no milliseconds, then I'd look to an upstream problem in the database-- in other words your query is returning something without milliseconds.

When you execute the exact same query in Management Studio, do you see milliseconds? My guess is this is a query-related issue. But am intrigued to find our more.

Here's how I'd try to troubleshoot this: step through in the debugger and look at the type and value of readerTimestamp". If the type is not SqlDateTime, that would make me suspicious-- I'd then look at the query to figure out why that column was returning another type instead of DATETIME (or DATETIME2, etc. ) if that value is SqlDateTime and it does contains milliseconds, then I'd look at the cast as the source of the problem.To validate this, I'd try (in debugger or code) SqlDataReader.GetDateTime() and SqlDataReader.GetSqlDateTime() to see if either returns the correct result. This admittedly seems really unlikely as source of the problem-- casting should work fine.

If that value from #1 is SqlDateTime but contains no milliseconds, then I'd look to an upstream problem in the database-- in other words your query is returning something without milliseconds. When you execute the exact same query in Management Studio, do you see milliseconds? My guess is this is a query-related issue.

But am intrigued to find our more.

The type of reader"Timestamp" is System. DateTime - attempting to cast into a SqlDateTime fails. – Justin Sep 22 '09 at 17:31 The query is a simple "SELECT * FROM ..." - the ... is in this case very complex, however I've executed the exact same query in Sql Server Management Studio and see then Milliseconds value.

– Justin Sep 22 '09 at 17:32 What do you get when you use reader.GetSqlDateTime()? Also, another possibility might be (if your "..." is a join) that there are two coulmns from different tables with the same name but with different values. That's easy to test by replacing * with one coulumn name in your query.

– Justin Grant Sep 22 '09 at 18:48 Turns out my problem was with the query after all - I had to capture a profiler trace and execute the exact query and the milliseconds value came up empty. – Justin Sep 22 '09 at 8:27.

Maybe this (Difference between DateTime in c# and DateTime in SQL server) will help a little.

That is because the default format string for DateTime doesn't include milliseconds. If you use a custom format, you will see the milliseconds value. An example: public class Program { private static string connString = @"Data Source=(local);Initial Catalog=DBTest;Integrated Security=SSPI;"; public static void Main(string args) { using (SqlConnection conn = new SqlConnection(connString)) { conn.Open(); using (SqlCommand cmd = new SqlCommand("SELECT * FROM MilliSeconds")) { cmd.

Connection = conn; SqlDataReader reader = cmd.ExecuteReader(); while(reader.Read()) { DateTime dt = (DateTime)reader"TimeCollected"; int milliSeconds = dt. Millisecond; Console. WriteLine(dt.

ToString("yyyy-MM-dd HH:mm:ss. Fff")); } } } Console.ReadLine(); } } From a database with these values: 1 2009-09-22 18:11:12.057 2 2009-09-22 18:11:28.587 3 2009-09-22 18:11:29.820 The resulting output from the code above is: 2009-09-22 18:11:12.057 2009-09-22 18:11:28.587 2009-09-22 18:11:29.820.

Sorry, its not that Timestamp. ToString("yyyy-MM-dd HH:mm:ss. Fff") returns 19:43:43.000 - note that the millisecond values are all 0 when they are 910 on the database.

– Justin Sep 22 '09 at 16:29 1 Well, I disagree with you. I have added the resulting output from my example code. – Magnus Johansson Sep 22 '09 at 16:38.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions