Derek Osborne
Derek Osborne

Reputation: 41

Include milliseconds in sql query from C#

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

Upvotes: 2

Views: 1496

Answers (2)

Derek Osborne
Derek Osborne

Reputation: 41

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");

Upvotes: 2

Bubblesphere
Bubblesphere

Reputation: 449

Cast your datetime variable to a string using

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

Upvotes: 0

Related Questions