简体   繁体   English

C# 日期时间问题到 Sql

[英]C# Datetime Issue to Sql

I am working on C# SQL.我正在研究 C# SQL。

I've a datetime value "1/10/2016 12:00:00 AM" for eg我有一个日期时间值"1/10/2016 12:00:00 AM" ,例如

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当我将此值作为参数传递给 SQL 存储过程时,它更改为10 Jan 2016

I need the result as 1 OCT 2016 in SQL.我需要 SQL 中的结果为1 OCT 2016 Sql stored procedure parameter data type is datetime. Sql 存储过程参数数据类型是日期时间。

Many Thanks非常感谢

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.如果仍然是 2016 年 1 月 10 日,那么存储过程采用的默认值可能会发生变化,即存储值为 2016 年 1 月 10 日。

Use parameters to pass the values to sql query.使用参数将值传递给 sql 查询。 Or use myDateTime.ToString("s");或者使用myDateTime.ToString("s"); this for datetime value这是日期时间值

"s" - Standard datetime format "s" - 标准日期时间格式

Well, are you sure that the value in c# is in fact 1 OCT 2016 and not 10 Jan 2016 ?那么,您确定 c# 中的值实际上是1 OCT 2016而不是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.在 c# 和 Sql Server 之间传递DateTime值的最佳实践是使用DateTimeDateTime2类型的 sql 参数并传递DateTime结构的实例。
If you are doing that, then the DateTime values are guaranteed to pass correctly.如果您这样做,则保证 DateTime 值正确传递。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM