.Net+MySQL组合开发(三) 乱码篇
使用过MySQL的朋友都知道有乱码问题困扰,而出现此问题都是因没有正确设置其编码造成;
假设现在要做一个简体中文网站;
一、设置数据库编码
安装mysql时可选择编码,如果已经安装过,可以更改文件my.ini(此文件在mysql的安装目录下)中的配制以达到目的;打开文件找到两处:
[client]
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
port=3306
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
[mysql]
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
default-character-set=gb2312
# The default character set that will be used when a new schema or table is
# created and no character set is defined
default-character-set=gb2312
更改红色部分为gb2312
此时新建数据库后,数据库目录下有个db.opt文件,内容如下:
default-character-set=gb2312
default-collation=gb2312_chinese_ci
编码数据库一致
二、客户端工具编码
![](https://www.cnblogs.com/images/cnblogs_com/chy710/mysqlado4.JPG)
编码设置与数据库
编码相同,可以用客户端工具直接写入数据,不产生乱码;
三、web.config中设置
连接字符串中的编码,MySQL Connector Net 5.0.3用
<connectionStrings>
<add name="MySqlServer" connectionString="Data Source=127.0.0.1;User ID=root;Password=123;DataBase=BOOK;Charset=gb2312"/>
</connectionStrings>
读取写入的编码
<globalization responseEncoding ="gb2312" requestEncoding ="gb2312"/>
public static CharacterSet GetChararcterSet(DBVersion version, string CharSetName)
{
CharacterSet cs = (CharacterSet)mapping[CharSetName];
if (cs == null)
throw new MySqlException("Character set '" + CharSetName + "' is not supported");
return cs;
}
private static void LoadCharsetMap()
{
mapping.Add("latin1", new CharacterSet("latin1", 1));
mapping.Add("big5", new CharacterSet("big5", 2));
![](https://www.cnblogs.com/Images/dot.gif)
.
}
假设现在要做一个简体中文网站;
一、设置数据库编码
安装mysql时可选择编码,如果已经安装过,可以更改文件my.ini(此文件在mysql的安装目录下)中的配制以达到目的;打开文件找到两处:
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
此时新建数据库后,数据库目录下有个db.opt文件,内容如下:
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
二、客户端工具编码
编码设置与数据库
编码相同,可以用客户端工具直接写入数据,不产生乱码;
三、web.config中设置
连接字符串中的编码,MySQL Connector Net 5.0.3用
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
OK,此时设置完成,做好的网站即一个简体中文网站,不会有乱码的;
如果需要做一个繁体网站,把以上设置编码的地方变成"gbk"即可。
常见问题:
Character set 'gbk' is not supported
出现此问题,十有八九可能你用了底版本的MySQL Connector ,因为在MySQL Connector 1.07中提供的字符编码是有限的,找到不到web.config中设置的编码类型,没有gbk,不过好像有big5,具体请看MySQL Connector 1.07源码中的CharSetMap.cs文件,其中
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
而MySQL Connector 5.03中提供的字符编码相当全面了,所以建议使用5.03,不过5.03与旧版本的mysql数据库有些地方不太兼容。在执行存储过程等大的数据操作时会出现:
Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding