Processing of ASP. netx garbled data written into MySQL

Source: Internet
Author: User

When using MySQL + Asp.net for development, the problem of garbled Chinese characters written into the database is a headache. In combination with my own situation, refer to the online materials. The solution is similar to the following three methods:

My database uses charset = gb2312 encoding, Asp.net is UTF-8, and JS uses UTF-8 encoding.

 

Method 1: Check the database Encoding

Check whether the default Character Set of the database, the character set of the table, and the character set of the field are consistent with the encoding of the written characters.

Method 2: Define the encoding method on the database connection string

 

Character Set charset = gb2312

 

For example: "Server = localhost; user = ***; database = SchoolNet; Port = 3306; Password = ***; charset = gb2312"

 

Method 3: If the problem persists, try again.

Run the database command:

DB. executenonquery ("set names gb2312 ")

Run the following command to modify the character set of the database:
Alter database da_name default Character Set 'charset '.
The client sends data in GBK format. The following configurations can be used:
Set character_set_client = 'gbk'

 

The reason is as follows:
Set character_set_connection = 'gbk'
Set character_set_results = 'gbk'
This configuration is equivalent to set names 'gbk '.
Perform operations on the database you just created
Mysql> use test;
Database changed
Mysql> insert into mysqlcode values (null, 'php hobby ');
Error 1406 (22001): Data too long for column 'content' at Row 1
If the character set is not specified as GBK, an error occurs during insertion.
Mysql> set names 'gbk ';
Query OK, 0 rows affected (0.02 Sec)
The specified character set is GBK.
Mysql> insert into mysqlcode values (null, 'php hobby ');
Query OK, 1 row affected (0.00 Sec)
Inserted successfully
Mysql> select * From mysqlcode;
+ ---- + ----------- +

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.