如何通过 ODBC 将 UTF8 文本从 ASP.NET 写入 MySQL?
我在共享主机上使用 MySQL 5,使用 MySQL 5.1 ODBC 驱动程序从 ASP.NET 3.5 进行连接。 我想存储 UTF8 字符串。 我的表曾经全部位于“latin1_swedish_ci”中,但我使用以下方法将数据库、表和列转换为UTF8
ALTER DATABASE `my_db` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ;
ALTER TABLE `my_table` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ;
ALTER TABLE `my_table` CHANGE `subject` `subjext` TEXT CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL
:和 ODBCCommand:
错误 [HY000] [MySQL][ODBC 5.1 驱动程序][mysqld-5.0.51b-community-nt]不正确的字符串值:'\xE3\x80\x80\xE6\x89\x8B...' 的“主题”列
请注意,由于我使用的是 5.1 驱动程序,因此我无法使用“SET NAMES utf8;” - 它会产生错误。
对于第 1行
I'm using MySQL 5 on shared hosting, connecting from ASP.NET 3.5 using the MySQL 5.1 ODBC driver. I'd like to store UTF8 strings. My tables used to be all in "latin1_swedish_ci", but I converted the the database, table, and column to UTF8 using:
ALTER DATABASE `my_db` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ;
ALTER TABLE `my_table` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ;
ALTER TABLE `my_table` CHANGE `subject` `subjext` TEXT CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL
But I still get this error when inserting non-ascii characters (like "遊ぶ")into my database using an ODBCConnection and ODBCCommand:
ERROR [HY000] [MySQL][ODBC 5.1 Driver][mysqld-5.0.51b-community-nt]Incorrect string value: '\xE3\x80\x80\xE6\x89\x8B...' for column 'subject' at row 1
Note that since I'm using the 5.1 driver, I can't use "SET NAMES utf8;" - it produces an error.
Any ideas what I'm missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
需要检查的一些事项:
确保您的表格和文本字段确实接受 utf8:
使用 MySQL 查询浏览器 并尝试手动编辑一些数据。< br>
如果保存编辑后保持正常,则字段和表格设置正确。
确保您实际上插入的是 utf8 兼容字符,而不是基于另一种编码形式的字符,例如中文的 GB2332。
如果是这种情况,您可能需要将字符串转换为 utf8,然后才能将它们发送到数据库。
您可以查看使用 Encoding 类在.Net中。
我不久前回答了一篇关于相关内容的帖子,并且有一个CodeProject 文章 就针对此问题。
您可能还需要确保ODBC 连接字符串 包括以下内容:
字符集=utf8;
有一个 所有参数的列表 您可以用于 ODBC 连接。
A few of things to check:
Make sure your tables and text fields really accept utf8:
Use the MySQL Query Browser and try to edit some data manually.
If it stays OK once you saved your edits, then the fields and tables are correctly set.
Make sure that you are actually inserting utf8 compliant-characters and not characters based on another form of encoding such as GB2332 for Chinese.
If that's the case, you may need to convert the strings to utf8 before you can send them to the database.
You can have a look at using the Encoding class in .Net.
I've answered a post on something related a while ago and there is a CodeProject article on just this issue.
You probably also need to make sure that the ODBC connection string includes the following:
CharSet=utf8;
There is a list of all the parameters you can use for the ODBC conection.