c# - 将参数传递给 MS-Sql 存储过程时出现 DateTimeFormat 问题

标签 c# sql-server-2008

我有一个存储过程,当我从前端传递值 DateTime.Now 时,它有一个类似 @CurrentDate datetime 的参数 (C#) 对于此存储过程,它工作正常。

但是当我在我的系统/服务器中将日期格式更改为English(India)时,DateTime.Now 将返回一个值如 25-10-2012 PM 05:23:27。我将此值传递给存储过程,但收到如下错误消息,

Msg 242, Level 16, State 3, Line 10
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.

注意:我不想使用来自 Sqlserver 的 GETDATE(),我想从 c# 传递参数。

我该如何解决这个问题?

最佳答案

日期格式的问题是因为您将其转换为字符串,然后再转换回 DateTime 值。根据特定服务器上的文化设置,这可能有效也可能无效。它还可能会误解数据,例如将日期从 2013-10-12 转换为 2013-12-10。

DateTime datet = new DateTime(year,month,day);
  startDateParam.Value = datet;
  endDateParam.Value = datet;

“请注意,服务器存储的日期时间格式为‘1900-01-01 00:00:00.000’”

关于c# - 将参数传递给 MS-Sql 存储过程时出现 DateTimeFormat 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13069338/

相关文章:

c# - 如何使用泛型开发适用于 double 和小数的代码

java - 创建到sql server 2008的java连接

sql - 比较两个数据库

sql - 如何解决 SQL Server 错误 "An expression of non-boolean type specified in a context where a condition is expected, near ' GROUP'"

c# - 如何在 c# 的 ms word 文档中设置文本方向 RightToLeft?

c# - 当复选框未选中时如何执行某些操作

sql-server - SQL Server 2008 SCOPE_IDENTITY() 问题

sql-server - 存储过程适用于 SQL Server 2000,但不适用于 SQL Server 2008R2

c# - Lambda 表达式作为参数用作 AsTask() 回调

c# - .net 中的 [] 括号是什么?