How to use c # To connect to MySQL with Chinese garbled characters

Source: Internet
Author: User
Tags mysql command line

This article describes the correct solution to the problem of connecting MySQL Chinese garbled characters using c #. We all know that MySQL Chinese garbled characters are a heart disease that troubles us all, so how can I use the c # connection to answer it correctly? The answer is as follows.

I don't know why, because the default configuration of the entire database is UTF-8, and my colleagues correctly executed it when using the http protocol to transmit UTF-8 Chinese characters.

So I looked for the explanation of the set names XX command and found a clear article, which was reproduced.

Recently, we have received BBT training to build a voting system. System code is not very difficult, but I spent most of my time studying character sets and encoding. Encoding character sets in MySQL and Apache systems.

The solution to these problems on the internet is relatively fragmented and one-sided. Most of the solutions are provided, but they do not tell why.

So I will summarize the gains over the past few days to avoid future detours. This article is helpful for PHP writing. After reading it, you will know how to make your PHP program display normally on the servers of most space providers ), however, more help lies in the setup and Setup of network servers.
Let's talk about the character set of MySQL. In Windows, you can modify the [code] # client section in my. ini.

 
 
  1. [mysql]   
  2. default-character-set=utf8   
  3. # SERVER SECTION   
  4. [mysqld]   
  5. default-character-set=utf8[/code]  

These two fields are used to change the default Character Set of the database. The first is the default Character Set of the client, and the second is the default Character Set of the server. Suppose we set both to utf8, and then input "show variebles like" character_set _ % ";" in the MySQL Command Line Client. The following characters are displayed:

 
 
  1. character_set_client latin1   
  2. character_set_connection latin1   
  3. character_set_database utf8   
  4. character_set_results latin1   
  5. character_set_server utf8   
  6. character_set_system utf8  

The utf8 is changed with the above settings. At this point, if we read data from the database through the PHP program using the UTF-8, it is likely to be a string of Or other MySQL Chinese characters are garbled.

After checking the data online for half a day, the solution is simple. After connecting to the database and reading the data, run a query "set names UTF8" first ", that is, in PHP, it is [code] mysql_query ("set names UTF8"); [/code], it can be displayed as long as the characters in the database information are normal ). Why? In this query, what is the function of "set names UTF8?

Enter "set names UTF8;" in the MySQL command line, and then execute "show variebles like" character_set _ % ";", the original latin1 variables "character_set_client", "character_set_connection", and "character_set_results" are all changed to utf8. Refer to the Manual. The above sentence is equal:

 
 
  1. [code]SET character_set_client = utf8;   
  2. SET character_set_results = utf8;   

SET character_set_connection = utf8; [/code] to check the functions of these three variables:

Information Input path: client → connection → server;

Information output path: server → connection → results.

In other words, each path must undergo three character set encoding changes. Take the output of MySQL Chinese garbled characters as an example. For utf8 data in the server, convert the input connection to latin1, convert the input results to latin1, and transfer the results to the UTF-8 page. If the two character sets are incompatible, such as latin1 and utf8, the conversion process is irreversible and destructive. So it cannot be turned back.

However, we need to declare that "set names UTF8" is only temporary, and the default value will be restored after MySQL is restarted. Next we will talk about the configuration of MySQL on the server. Isn't it because we have to add "SET NAMESUTF8" to each database read/write to ensure Data Transmission Encoding consistency? Can we configure MySQL to achieve the three variables which are the character set we want by default? I did not mention the manual, and I did not find the answer online. Therefore, the line of code cannot be omitted from the server configuration perspective.

Conclusion: To enable the normal display of Your webpage on more servers, add "set names UTF8", even if you do not add this sentence, you can access the webpage normally.

Original article title: c # how to connect to MySQL with Chinese garbled characters

Connection: http://www.cnblogs.com/13590/archive/2010/04/17/1714157.html

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.