从 DataReader 访问 DateTime 字段时出现 MySqlConversionException
我有一个基于 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果 MySQL 数据库中的日期时间值为零 (00/00/0000 00:00),有时会出现此错误。 尝试将其添加到连接字符串的末尾:
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:
在 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.
我建议这可能是一个特定于文化的错误 - 应用程序是否与数据库位于同一服务器上,它们是否具有相同的文化设置?
另外,该列肯定是 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?
它也可以是 DBNull 值。
It could also be a DBNull value.