C # It is added by using Winform to operate MYSQL. garbled data is read.

Source: Internet
Author: User
Tags mysql tutorial

1. C # connect to MYSQL using Winform to add and read MySQL. mySql. data components for Data connection and operations 2. mysql versions earlier than 4.01 cannot be configured for the Mysql Environment

1. C # Use Winform to connect to MYSQL and read Data from MySQL. Use the Mysql. MySql. Data component to connect to and operate MYSQL.
2. mysql tutorial versions earlier than 4.01 cannot be configured for the Mysql Environment

Source: http://www.dezai.cn/blog/article.asptutorial? Id = 418
Symptom:
1. The new data operation is normal, but the data in the corresponding table in the MYSQL database tutorial is inserted as question marks.

2. The Chinese data in the Mysql table is displayed normally, but it is read and displayed through the datagridview? No.

Solution Process

1. in mysql 4.0.1, using the set names 'gb2312' statement does not work. Similarly, changing the field or character set of the corresponding table does not seem to work, versions later than 5.0 have not been used, so we cannot provide a normal statement. Please try again.

Mysql modifies the character set of tables, fields, and libraries.

Http://www.dezai.cn/Channel/Code/Detail.aspx? CodeID = d3401000-2abd-4654-be6f-aa7c65c64ff1


2. Adding Character Set = utf8 or charset = gb2312 to the string connecting to mysql still works.
Public static readonly string strSMS = "Data Source = 188.28.1.36; user id = sale; password = sale; database = mas; Character Set = utf8 ;"

3. for B/S mode, try to save the page as UTF-8 format, or ANSI format

4. The following two methods solve my big problem:

Program code

// Convert data when writing data to the database
Public string GB2312_ISO8859 (string write)
{
// Declare the character set
System. Text. Encoding iso8859, gb2312;
// Iso8859
Iso8859 = System. Text. Encoding. GetEncoding ("iso8859-1 ");
// 2312 GB
Gb2312 = System. Text. Encoding. GetEncoding ("gb2312 ");
Byte [] gb;
Gb = gb2312.GetBytes (write );
// Return the converted characters
Return iso8859.GetString (gb );
}

// Convert data when reading data
Public string ISO8859_GB2312 (string read)
{
// Declare the character set
System. Text. Encoding iso8859, gb2312;
// Iso8859
Iso8859 = System. Text. Encoding. GetEncoding ("iso8859-1 ");
// 2312 GB
Gb2312 = System. Text. Encoding. GetEncoding ("gb2312 ");
Byte [] iso;
Iso = iso8859.GetBytes (read );
// Return the converted characters
Return gb2312.GetString (iso );
}

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.