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;
+ ---- + ----------- +