Home >Database >Mysql Tutorial >mysql5插入乱码问题_MySQL

mysql5插入乱码问题_MySQL

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-01 13:35:10840browse

bitsCN.com

mysql5插入乱码问题

 

在数据库由4.2升级到5.1.6-comm...之后 都是latin1的默认编码, 以前的程序插入中文乱码 ,以前的写法:在source中加入charset=gb2312,然后在插入前执行set names gb2312即可;

 

在网上找了好多文章,除了上面的写法外 ,就是直接更改数据库的设置,但是这个数据库是买的别人的产品 ,不能随便乱动,只好在代码上作文章, 折腾了好久之后 ,得出以下结论与大家共享,

 

latin1即是iso8859-1, 所以在插入之前需要将内容转换为iso8859-1, 如下:

 

Encoding iso88591 = Encoding.GetEncoding("iso8859-1");

Encoding df = Encoding.Default;

 

byte[] gb2312bytes = df.GetBytes(content);

 //byte[] asciiBytes = Encoding.Convert(df, iso88591, gb2312bytes);

tring str = iso88591.GetString(gb2312bytes);

 

 这样转换之后 ,source中不用加入charset参数 ,程序中也不需要先执行set names **, 即可正常插入

bitsCN.com
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn