1

I am currently using SqlDataAdapter to query a SQL database:

SqlCommand myCommand = new SqlCommand();
myCommand.Connection = myConnection;
string cmd = @"select * from dbo.Table where errordate > '2015-05-29'";
myConnection.Open();
SqlDataAdapter da = new SqlDataAdapter(cmd, sqlConn);
da.Fill(dt);

The query is working fine but the only problem is that The date column does not include milliseconds. When I perform the same query with MS SQL server management studio, the milliseconds are there. Why wouldn't the milliseconds be present in C#?

Thanks

  • Just the milliseconds ?? it shouldn't consider the time part, **Do not pass string date, instead pass .Net `DateTime` as SqlParameter** – Habib Jun 01 '15 at 16:20
  • What is the data type of the column `errordate`? And when you say *"Why wouldn't the milliseconds be present in C#?"* - How are you validating this? – GarethD Jun 01 '15 at 16:25
  • 3
    Do you mean the `SqlDataAdapter` set does not have `milliseconds` when you show it on the screen? That is likely because `DateTime.ToString()` defaults to region-specific date/time. See [this MSDN topic](https://msdn.microsoft.com/en-us/library/k494fzbf(v=vs.110).aspx) on the subject. – Der Kommissar Jun 01 '15 at 16:27
  • Habib, I want the full date and time including the milliseconds. Could you include an example of how to pass a DateTime as an Sql parameter? – Derek Osborne Jun 01 '15 at 16:27
  • Please refer to the below post http://stackoverflow.com/questions/1181662/is-there-any-difference-between-datetime-in-c-sharp-and-datetime-in-sql-server – codingpirate Jun 01 '15 at 16:55

2 Answers2

1

After reading the comments above I realized the issue was not with the SQL query but with the way I accessed the data from the DataTable. This did the trick:

DateTime date = (DateTime)dr[2];
string ds = date.ToString("MM/dd/yy HH:mm:ss.fff");
0

Cast your datetime variable to a string using

[YourDate].ToString("MM/dd/yyyy HH:mm:ss.fff", CultureInfo.InvariantCulture);
Bubblesphere
  • 449
  • 4
  • 11