带有日期时间参数问题的 ADO.NET 更新命令

发布于 2024-08-20 08:22:04 字数 742 浏览 1 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夏尔 2024-08-27 08:22:04

当然,这会在 SQL Server 2005 中引发错误:

print cast('2010-02-03 15:26:54.3100000' as datetime)

而这工作正常:

print cast('2010-02-03 15:26:54.31' as datetime)

但是...aObject.ADateTime 不是 DateTime 类型吗?听起来它正在作为字符串传递给存储过程。

如果它确实是一个字符串,我建议在传入之前将其转换为 DateTime:

nm8.Value = DateTime.Parse(aObject.ADateTime);

Certainly this throws an error in SQL Server 2005:

print cast('2010-02-03 15:26:54.3100000' as datetime)

whereas this works fine:

print cast('2010-02-03 15:26:54.31' as datetime)

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:

nm8.Value = DateTime.Parse(aObject.ADateTime);
盗梦空间 2024-08-27 08:22:04

您必须针对基类进行编程还是可以使用 SqlParameter? (假设您正在使用连接到Sql Server)也许您可以尝试设置SqlDbType属性

SqlParameter sqlParam = (SqlParameter)nm8; //check if it's compatible before casting.
sqlParam.SqlDbType = SqlDbType.DateTime

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

SqlParameter sqlParam = (SqlParameter)nm8; //check if it's compatible before casting.
sqlParam.SqlDbType = SqlDbType.DateTime
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文