. Net + MySQL combined development (3) Garbled text

Source: Internet
Author: User
Tools used

MySQL5.022
VS2005 Team Suite
MySQL Connector Net 5.0.3
EMS SQL Manage 2005 For MySQL

Friends who have used MySQL know that there is a garbled problem, and this problem is caused by incorrect encoding;
Suppose you want to create a simplified Chinese website;
1. Set database Encoding
You can select the encoding when installing mysql. If you have already installed mysql, you can change the file my. ini (this file is in the mysql installation directory) for the purpose. open the file and find the following two parts: [client]

Port = 3306

[Mysql]

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

Change the red part to gb2312.
After the database is created, there is a db. opt file in the database directory. The content is as follows: default-character-set = gb2312
Default-collation = gb2312_chinese_ci

Consistent encoding Database
Ii. Client tool code

Encoding settings and databases
The codes are the same. You can use a client tool to directly write data without garbled characters;
3. Set in web. config
Encoding in the connection string. For MySQL Connector Net 5.0.3, use <connectionStrings>
<Add name = "MySqlServer" connectionString = "Data Source = 127.0.0.1; User ID = root; Password = 123; DataBase = BOOK; Charset = gb2312"/>
</ConnectionStrings>

Read Write encoding <globalization responseEncoding = "gb2312" requestEncoding = "gb2312"/>

OK. Now the configuration is complete. The website is a simplified Chinese website and no garbled code is found;
If you need to create a traditional Chinese website, change the above encoding to "gbk.

FAQs:
Character set 'gbk' is not supported
If this problem occurs, you may have used the MySQL Connector of the base version, because the character encoding provided in MySQL Connector 1.07 is limited and the web cannot be found. the encoding type set in config, no gbk, but it seems that there is big5. For details, see CharSetMap in MySQL Connector 1.07 source code. cs file, where

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 ));
.
}

MySQL Connector 5.03 provides comprehensive character encoding, so 5.03 is recommended, but 5.03 is not compatible with the old mysql database. When performing large data operations such as stored procedures, the following occurs:
Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.