JMK
JMK

Reputation: 37

C# Datetime Issue to Sql

I am working on C# SQL.

I've a datetime value "1/10/2016 12:00:00 AM" for eg

Datetime dt="1/10/2016 12:00:00 AM"

When I passed this value to SQL stored procedure as parameter, it changed to 10 Jan 2016

I need the result as 1 OCT 2016 in SQL. Sql stored procedure parameter data type is datetime.

Many Thanks

Upvotes: 1

Views: 306

Answers (3)

Zohar Peled
Zohar Peled

Reputation: 82534

Well, are you sure that the value in c# is in fact 1 OCT 2016 and not 10 Jan 2016?
The row:

Datetime dt = "1/10/2016 12:00:00 AM";

Doesn't even compile.

Best practice for passing DateTime values between c# and Sql Server is using sql parameters of type DateTime or DateTime2 and passing an instance of the DateTime struct.
If you are doing that, then the DateTime values are guaranteed to pass correctly.

Upvotes: 0

s.k.Soni
s.k.Soni

Reputation: 1310

Change the datetime value and see what happened. If still coming the 10 Jan 2016, then it may be changes that stored procedure is taking the default value which is store value is 10 Jan 2016.

Upvotes: 1

kgzdev
kgzdev

Reputation: 2885

Use parameters to pass the values to sql query. Or use myDateTime.ToString("s"); this for datetime value

"s" - Standard datetime format

Upvotes: 0

Related Questions