MySQL ODBC 驱动程序 5.2w 导致日期时间错误 5.1.10 工作正常

标签 mysql odbc driver dsn

当我使用 MySQL 驱动程序 5.1.10 运行托管在 IIS 中的 c# Web 服务时,它运行完美,但是当我更新到最新的驱动程序 5.2 时,出现以下错误

ERROR [HY000] [MySQL]ODBC 5.2(w) Driver][mysqld-5.5.28-log]Incorrect datetime value:      '2012-12-14 14:01:13.553000000' for column 'Date' at row 1

我正在使用系统 DSN。

我在 Windows 7 上运行 MySQL 服务器版本 5.5 x64,它试图将值保存到的数据类型是 DateTime,如错误消息所示。还有其他人遇到过这个问题吗?

最佳答案

我遇到过同样的问题,而且以前版本的 ODBC 连接器(5.1.10 及更早版本)似乎对 C# DateTime 字段的处理非常不同。可能是连接器的早期版本只是截断了足够多的 DateTime 以使其在 MySQL 中工作,而当前版本保留了更多的小数数据。

我还注意到 Decimal 变量的一些奇怪行为使连接器完全崩溃,没有任何描述性错误消息。我的解决方案是改用 Double 类型。

编辑 我找到了 changelog我之前看到过 ODBC/Connector v5.1.11,它证实了 Bugs Fixed 列表中 DateTime 值的不同处理方式:

Fractional seconds part of timestamp was ignored in prepared statements that use SQLBindParameter and SQL_C_TIMESTAMP type. For example, a prepared query comparing two timestamp values that only differed in the fractional part would consider the values identical. (Bug #12767761, Bug #60648)

有趣的是直到MySQL 5.6.4 DATETIME 和 TIMESTAMP 字段只能精确到秒(没有毫秒或微秒),因此连接器以前版本的“不正确”行为忽略了小数秒,MySQL 很高兴。由于连接器现在不忽略小数数据,因此如果通过存储过程或准备语句传入此数据,则会生成错误。所以目前的解决方案是:

1) 更新到 MySQL 5.6 (现在是 GA)

2) 使用 neat techniqueDateTime 中剥离小数数据,同时仍保持类型及其所有其他属性:

DateTime dt = DateTime.Now //Or any existing DateTime value
dt = dt.AddTicks( - (dt.Ticks % TimeSpan.TicksPerSecond));

3) 如果您根本不想处理 C# 和 DateTime 类型的细微差别,并且知道在使用数据库时使用字符串数据的潜在安全后果,您可以始终构建自己的日期字符串或使用 DateTimeToString 方法并将字符串传递给 MySQL:

// convert DateTime to string with formatting for MySQL
string dateformysql = mydatetime.ToString("yyyy-MM-dd HH:mm:ss");

引用资料:
How to truncate milliseconds off of a .NET DateTime
Why doesn't MySQL support millisecond / microsecond precision?
ODBC Connector 5.1.11 Changelog

关于MySQL ODBC 驱动程序 5.2w 导致日期时间错误 5.1.10 工作正常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13886757/

相关文章:

mysql - 如何在 WHERE 条件下使用存储函数

java - 如何使用 JDBC 连接到 Solaris 操作系统中的 Oracle DB?

php - 找不到类 MongoDB

sql-server - SQL Server 导入和导出向导错误 : "Index was outside the bounds of the array" via 32bit ODBC data source?

linux-kernel - GPIO和SPI有什么关系?

java - 找不到适合 jdbc :postgresql://192. 168.1.8:5432/NexentaSearch 的驱动程序

mysql语法错误代码1064不确定该怎么办

python - 如何实时读取html并循环插入MySQL?

php - Doctrine 2 : how to auto-get data of related tables?

.net - 使用 Microsoft ODBC for Oracle 连接到 Oracle 10g 数据库