C # Chinese garbled characters connecting to MySQL

Source: Internet
Author: User
Tags mysql command line

For example, the question is still worrying. There are also a lot of answers on the internet, varied, and I don't know who is right or who is wrong. I personally think there are several articles to solve this problem well. The following is a summary:

 

Article 1:


C # connect to MySQL to solve Chinese Character garbled characters

I suddenly found that all the Chinese characters in my data table were garbled and the English language was normal. I suspected it was a coding problem.

In my colleagues' work, I used HTTP to send UTF-8 encoded Chinese characters. The server program can correctly receive and store the characters in the database and read them.

Configure the data table as UTF-8

 

At first, we thought about the encoding format of the local language and encoded the string, for example;

 

 

String STR = "Chinese character encoding ";

Byte [] bytes = encoding. utf8.getbytes (STR );
String newstr = encoding. utf8.getstring (bytes );

 

Subsequent debugging is useless.

Add the following when connecting to the database:

Mysqlcommand setformat = new mysqlcommand ("set names gb2312", m_connection );
Setformat. executenonquery ();
Setformat. Dispose ();

Done.

At first, 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.

 

[The following part is reprinted]

There is also an article about Apache and PHP coding: http://www.phpchina.com/bbs/thread-13860-1-1.html

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. The encoding (character set) problem between MySQL and Apache makes me suffer. 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 a little helpful for PHP writing (you will know after reading it, how to make your PHP Programs Display normally on 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.

[MySQL]

Default-character-set = utf8

# Server section

[Mysqld]

Default-character-set = utf8 [/Code] 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:
Character_set_client Latin1
Character_set_connection Latin1
Character_set_database utf8
Character_set_results Latin1
Character_set_server utf8
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 garbled characters. 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 ", [Code] mysql_query ("set names utf8") in PHP; [/Code] can be displayed normally (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 equivalent to: [Code] Set character_set_client = utf8;

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. Taking garbled output as an example, for utf8 data in the server, the incoming connection is converted to Latin1, the incoming results is converted to Latin1, And the UTF-8 page is converted to results. 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: In order to make your webpage display normally on more servers, add "set names utf8", even if you do not add this sentence.
A lot of questions. Thank you for your correction!

 

【]

However, I found that I was modifying set names gb2312 to utf8, but I could not write Chinese characters. The Chinese character field is empty and I don't know why

 

 

Article 2 C # tips for developing MySQL Chinese garbled characters

 

The following articles mainly introduce the correct solution for C # To develop MySQL Chinese garbled characters, including the development environment and related problem descriptions, and the description of the Code that needs to be inserted into the test data during MySQL Chinese garbled processing in C # development. The following describes the main content of the article.

Development Environment:

 
 
  1. vs 2008+easyphp5.3.0+win7 

Problem description: The Chinese characters inserted in MySQL are displayed as question marks, and reading is also displayed as question marks.

 
 
  1. C # The component used to access the database is MySQL. Data. dll (5.0.8.1)

The code for creating a test table is as follows (note that you should change the code to utf8_general_ci when creating a database ):

 
 
  1. CREATE TABLE TT (  
  2. id smallint(6) NOT NULL DEFAULT '1',  
  3. name varchar(64) NOT NULL,  
  4. descr varchar(64) DEFAULT NULL,  
  5. PRIMARY KEY (id)  

C # Insert the test data code into MySQL Chinese Garbled text processing:

 
 
  1. Insert into config TT ('4', 'hha ', 'haha ')

C # program code: Query part of the code

 
 
  1. MySQLConnection con = new MySQLConnection("server=127.0.0.1;uid=root;pwd=;database=test;Charset=utf8");  
  2. con.Open();  
  3. DataSet ds = new DataSet();  
  4. MySQLDataAdapter adp = new MySQLDataAdapter("select * from TT",con);  
  5. adp.Fill(ds);  
  6. con.Close();  
  7. dataGridView1.DataSource = ds.Tables[0]; 

Insert some code:

Insert some code

 
 
  1. Mysqlconnection con = new mysqlconnection ("Server = 127.0.0.1; uid = root; Pwd =; database = test; charset = utf8 ");
  2. Con. open ();
  3. String SQL = "insert into config TT ('4', 'hha ', 'haha ')";
  4. Mysqlcommand cmd = new mysqlcommand (SQL, con );
  5. Cmd. executenonquery ();
  6. Con. Close ();

The final effect is as follows:

The above content describes how to process MySQL Chinese garbled characters in C # development, hoping to help you in this aspect.

 

Article 3

C # solutions to MySQL Chinese garbled characters

Misunderstanding:
In the past few days, the optional encoding methods for searching online resources are: gb2312, GBK, and utf8 support dual-byte encoding types. This is not the case in actual experiments.
A. We all want to use utf8, but utf8 is not ideal for Chinese processing, and select, update, and insert are garbled;
B. the GBK character set is much larger than the gb2312 character set. It is the second preferred character set. However, after the server/database is set to the GBK character set, the connection in the program fails (not support), not to mention the operation;
Final Solution:
1. gb2312 Encoding
2. Set the default encoding in the my. ini file.
Add or modify default_charater_set = gb2312 in the [MySQL] and [mysqld] configuration segments, respectively.
3. When creating a database, use gb2312 encoding.
4. When creating a table, use gb2312 encoding and gb2312_chines_ci for collation (gb2312_bin is not tested)
5. Set the encoding for specific fields that need to store Chinese characters using gb2312 and gb2312_chines_ci for collation (gb2312_bin is not tested)
Improvement:
If you do need to store GBK encoded characters, run set names GBK after the connection is successful.

 

 

The second method is used for the problem I encountered.Charset = utf8OK.

"Server = 127.0.0.1; uid = root; Pwd =; database = test; charset = utf8"

 

 

 

 

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.