C # is using the WinForm operation MySQL New, read data garbled

Source: Internet
Author: User
Tags character set mysql tutorial mysql database

1.c# Winform connect MySQL to new MySQL, read data operation, use Mysql.MySql.Data component for data connection and operation
2.mysql Tutorial version of the lower 4.01 cannot be configured for the MySQL environment

Source: http://www.dezai.cn/blog/article.asp tutorial? id=418

Phenomenon:
1. New data normal operation, but insert MySQL database tutorial in the corresponding table data in the form of a question mark


The Chinese data displayed in the 2.Mysql table is normal, but the display is read by DataGridView.

Resolution process

1. In the version of MySQL 4.0.1 use set names ' gb2312 ' this statement is not feasible, but also to change the corresponding table of the field or character set seems to have no effect, more than 5.0 of the version has not been used, so can not provide a normal statement, you may try

MySQL Modify the character set of tables, fields, libraries

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


2. The string attached to MySQL plus character Set=utf8 or charset=gb2312 should still be useful.

public static readonly String strsms = "Data source=188.28.1.36;user id=sale;password=sale;database=mas; Character Set=utf8; "


3. If it is b/s mode, you can try to save the page as UTF-8 format, or ANSI

4. The following two methods solve my big problem

Program code


//conversion when writing to the database
public string gb2312_iso8859 (string write)
{ br>//Declaration Character Set
System.Text.Encoding iso8859, gb2312;
//iso8859
iso8859 = System.Text.Encoding.GetEncoding ("iso8859-1");
//GB 2312
gb2312 = System.Text.Encoding.GetEncoding ("gb2312");
Byte[] GB;
GB = gb2312. GetBytes (write);
//Returns the converted character
return iso8859. GetString (GB);
}

//When read out
public string iso8859_gb2312 (string read)
{
//declaring character set
Syste M.text.encoding iso8859,gb2312;
//iso8859
iso8859 = System.Text.Encoding.GetEncoding ("iso8859-1");
//GB 2312
gb2312 = System.Text.Encoding.GetEncoding ("gb2312");
Byte[] ISO;
ISO = iso8859. GetBytes (read);
//Returns the converted character
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.