从 DataReader 访问 DateTime 字段时出现 MySqlConversionException

发布于 2024-07-16 10:17:19 字数 576 浏览 10 评论 0原文

我有一个基于 MySql 的 C# 应用程序,使用 MySQL Connector; 我正在尝试做一个 DataReader 请求,查询执行得很好,但是,当尝试访问 DateTime 字段时,我收到 MySqlConversionException {"Unable to conversion MySQL date/time value to System.DateTime"}

这是原型

if (dr != null && !dr.Read()) return;

sesion.Id = Convert.ToInt32(dr["id"]);
sesion.Usuario = Convert.ToInt32(dr["usuario"]);
sesion.Estado = Convert.ToByte(dr["estado"]);
// doesn't work
sesion.FchCreacion = Convert.ToDateTime(dr["fch_creacion"]);

有什么建议吗? 提前致谢

I have a C# application over MySql, using MySQL Connector; I'm trying to make a
DataReader request, the query executes fine, however, when trying to access a DateTime field, i'm getting MySqlConversionException {"Unable to convert MySQL date/time value to System.DateTime"}

this is the prototype

if (dr != null && !dr.Read()) return;

sesion.Id = Convert.ToInt32(dr["id"]);
sesion.Usuario = Convert.ToInt32(dr["usuario"]);
sesion.Estado = Convert.ToByte(dr["estado"]);
// doesn't work
sesion.FchCreacion = Convert.ToDateTime(dr["fch_creacion"]);

Any suggestions?
Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

随遇而安 2024-07-23 10:17:19

如果 MySQL 数据库中的日期时间值为零 (00/00/0000 00:00),有时会出现此错误。 尝试将其添加到连接字符串的末尾:

Allow Zero Datetime=true

This error sometimes occurs if you have zero datetime values in your MySQL database (00/00/0000 00:00). Try adding this to the end of your connection string:

Allow Zero Datetime=true
酒浓于脸红 2024-07-23 10:17:19

在 MySQL 日期/时间和 .NET DateTimes 之间转换时存在一些潜在的问题,但有一个 MySQL 文档中的有用部分,提供有关如何处理问题的建议。

There are a few potential gotchas when converting between MySQL dates/times and .NET DateTimes, but there's a useful section in the MySQL documentation with advice on how to handle the issues.

水溶 2024-07-23 10:17:19

我建议这可能是一个特定于文化的错误 - 应用程序是否与数据库位于同一服务器上,它们是否具有相同的文化设置?

另外,该列肯定是 MySQL 中的日期时间吗?

I'd suggest it may be a culture specific error - is the applicaiton on the same server as the DB, and do they have the same culture settings?

Also, is the column definitely a datetime in MySQL?

各空 2024-07-23 10:17:19

它也可以是 DBNull 值。

It could also be a DBNull value.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文