Asp.net uses Mysql for garbled Processing

Source: Internet
Author: User

When asp.net is dealing with the mysql database, due to configuration problems, it may encounter a self-written method when reading data in the database, English, numbers can pass normally, however, you cannot use Chinese characters. If you use logon as an example (the method is omitted), you can log on normally when you enter the english user name. However, if you enter the noon user name, an error is returned. The solution is as follows:
1. Find my In the mysql database installation directory. INI file, change the value of default-character-set to default-character-set = gb2312 (modify two places), save and restart.
2. Find the web. config file of the asp.net file and add <globalization requestEncoding = "gb2312" responseEncoding = "gb2312"/>
3. If the string connecting to the database is written on the web. in config, add charset = gb2312 (for example, <add key = "test" value = "server = localhost; database = dbtest; uid = root) to the connection string; pwd = root; charset = gb2312 "/>)
4. recompile.
Sometimes the data displayed in the database is noon, but garbled characters are generated after the page is read. When writing a storage project, some Chinese characters appear and compilation fails, my solution is to convert all Chinese files into hexadecimal and then read them out. The Code is as follows:
Mysql:
Drop procedure if exists proc_hm_holiday_top5;
Create procedure proc_hm_holiday_top5 ()
Begin
Select *, (select userinfo_name from hm_userinfo where userinfo_id = holiday_uid) as username,
Case
When holiday_type = 1 and holiday_typeTwo = 1 then _ utf8 0xE8AFB7E581872FE79785E5818720
When holiday_type = 1 and holiday_typeTwo = 2 then _ utf8 0xE8AFB7E581872FE4BA8BE5818720
When holiday_type = 1 and holiday_typeTwo = 3 then _ utf8 0xE8AFB7E581872FE4B8A7E5818720
When holiday_type = 1 and holiday_typeTwo = 4 then _ utf8 0xE8AFB7E581872FE5A99AE5818720
When holiday_type = 1 and holiday_typeTwo = 5 then _ utf8 0xE8AFB7E581872FE4BAA7E5818720
When holiday_type = 1 and holiday_typeTwo = 6 then _ utf8 0xE8AFB7E581872FE5B9B4E5818720
When holiday_type = 1 and holiday_typeTwo = 7 then _ utf8 0xE8AFB7E581872FE585B6E4BB9620
When holiday_type = 2 then _ utf8 0xE58AA0E78FAD
When holiday_type = 3 then _ utf8 0xE587BAE5B7AE
When holiday_type = 4 then _ utf8 0xE7A7BBE4BC91
End
As holiday_class from hm_holiday where 1 = 1 order by holiday_addTime desc limit 5;
End;
C # code (convert Chinese to hexadecimal)
Public string GetHexFromChs (string s)
{
If (s. Length % 2 )! = 0)
{
S + = ""; // Space
}
System. Text. Encoding chs = System. Text. Encoding. GetEncoding ("UTF-8 ");
Byte [] bytes = chs. GetBytes (s );
String str = "";
For (int I = 0; I <bytes. Length; I ++)
{
Str + = string. Format ("{0: X}", bytes [I]);
}
Return str;
}
Private void button#click (object sender, EventArgs e)
{
String shuru = textBox1.Text. Trim (). ToString ();
String str = GetHexFromChs (shuru );
TextBox2.Text = str. Trim (). Trim ();
}
 
This is an excerpt from my blog

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.