最近几天打算系统的学习一下MVC,使用的是mysql数据库,在过程中发现一遇到中文就有乱码的问题,按照网上的说明将mysql服务器端和客户端的编码都设置成了utf8,还是不行,查询了资料后知道客户端的编码是跟客户机有关的。我的机器是WIN7,所以设置时改成了character_set_client,
character_set_results,character_set_connection也设置成了UTF8格式,后来我将这三个改成了gb2312后中文显示正确,且添加修改的内容也是正常的。资料上说应该将character_set_client,character_set_results,character_set_connection这三个设置为和server一样的编码集,在使用sql语句时使用set names gb2312,但我觉得每次使用语句都用此语句明显不合适,EF能不能这样我暂时还没有试。。。。目前就试验到这里。。。如有不对请各位大佬指证。
更新一下,asp.net 数据库连接字符串:connectionString="data source=localhost;database=XXX;user id =XXX;password=XXXX;charset=utf8;"
必须增加 charset=utf8
my.ini 添加以下内容以后重启mysql服务
# For advice on how to change settings please see # http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html # *** DO NOT EDIT THIS FILE. It's a template which will be copied to the # *** default location during install, and will be replaced if you # *** upgrade to a newer version of MySQL. [mysqld] # Remove leading # and set to the amount of RAM for the most important data # cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%. # innodb_buffer_pool_size = 128M # Remove leading # to turn on a very important data integrity option: logging # changes to the binary log between backups. # log_bin # These are commonly set, remove the # and set as required. # basedir = ..... # datadir = ..... # port = ..... # server_id = ..... # Remove leading # to set options mainly useful for reporting servers. # The server defaults are faster for transactions and fast SELECTs. # Adjust sizes as needed, experiment to find the optimal values. # join_buffer_size = 128M # sort_buffer_size = 2M # read_rnd_buffer_size = 2M sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES character-set-server=utf8 collation-server=utf8_general_ci [mysql] default-character-set = utf8 [mysql.server] default-character-set = utf8 [mysqld_safe] default-character-set = utf8 [client] default-character-set = utf8