如何解决MySQL插入部分中文字符的报错问题

时间:2021-10-25 07:46:18

Winform程序,进行添加功能,保存时,系统给出异常信息”Incorrect string value: ‘\xACQ\xCE\xC4\xD7\xAA…’ for column ‘TaskDes’ at rows“,在网上查了很多资料,最终解决了,数据库方面又长见识了,高兴!
问题原因:字符集的设置不一致导致
解决方法:需将客户端、服务器、数据库、表、字段的字符集设置成一致,具体如下:
1、修改MySQL的my.ini文件的信息,添加以下信息(红色部分)

[client]
port=3306
default-character-set=utf8
[mysql]
default-character-set=utf8
character_set_server=utf8
# SERVER SECTION
# -------------------------------------------------------------
# The following options will be read by the MySQL Server. Make sure that
# you have installed the server correctly (see above) so it reads this
# file.
#
[mysqld]
# The TCP/IP Port the MySQL Server will listen on
port=3306
#Path to installation directory. All paths are usually resolved relative to this.
basedir="D:/Program Files/MySQL/MySQL Server 5.5/"
#Path to the database root
datadir="C:/Documents and Settings/All Users/Application Data/MySQL/MySQL Server 5.5/Data/"
# The default character set that will be used when a new schema or table is
# created and no character set is defined
character_set_server=utf8

2、修改数据库的字符集:SET character_set_database=utf8;
3、修改表的字符集:ALTER TABLE 表名 CONVERT TO CHARACTER SET utf8;