带有日期时间参数问题的 ADO.NET 更新命令
我正在尝试使用 ADO.NET 通过存储过程进行数据库更新。
基本上我设置了所有参数和命令,并设置了如下所示的参数之一
DbParameter nm8 = provider.CreateParameter();
nm8.ParameterName = "@EDITDATE";
nm8.DbType = System.Data.DbType.DateTime;
nm8.Value = aObject.ADateTime;
command.Parameters.Add(nm8);
在存储过程中,输入参数定义为
@EDITDATE datetime = null,
,基本上存储过程所做的只是获取一条记录并使用传入的 EDITDATE 更新它但
我收到这个错误
将数据类型 varchar 转换为 datetime 时出错。
我发现日期时间值被传递到存储过程中,如下所示,
2010-02-03 15:26:54.3100000
我
2010-02-03 15:26:54.310
认为这就是导致转换错误的原因。
所以我的问题是为什么 ado.net 将日期时间转换为这种格式? 如何在不将值作为字符串传递的情况下解决问题。
多谢。
I am trying do a database update via stored procedure using ADO.NET.
Basically i setup all the parameter and command and have the one of the parameter set like the following
DbParameter nm8 = provider.CreateParameter();
nm8.ParameterName = "@EDITDATE";
nm8.DbType = System.Data.DbType.DateTime;
nm8.Value = aObject.ADateTime;
command.Parameters.Add(nm8);
In the stored procedure, the input parameter is defined as
@EDITDATE datetime = null,
and basically what the stored proc does is just get a record and update it with the EDITDATE passed in.
but i am getting this error
Error converting data type varchar to datetime.
and what i found was that the datetime value is passed in to the stored procedure as something like the following
2010-02-03 15:26:54.3100000
instead of
2010-02-03 15:26:54.310
and i think that's what is causing the casting error.
so my question is why ado.net convert the datetime in that format?
how can I resolve the issue without passing the value in as string.
thanks a lot.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当然,这会在 SQL Server 2005 中引发错误:
而这工作正常:
但是...
aObject.ADateTime
不是 DateTime 类型吗?听起来它正在作为字符串传递给存储过程。如果它确实是一个字符串,我建议在传入之前将其转换为 DateTime:
Certainly this throws an error in SQL Server 2005:
whereas this works fine:
But ... is
aObject.ADateTime
not of type DateTime? It sounds like it's being passed to the stored procedure as a string.If it is indeed a string, I would suggest converting it to DateTime before you pass it in:
您必须针对基类进行编程还是可以使用 SqlParameter? (假设您正在使用连接到Sql Server)也许您可以尝试设置SqlDbType属性
Do you have to program against the base class or can you use SqlParameter? (assuming you are using connecting to Sql Server) Maybe you can try setting the SqlDbType property