How to avoid garbled characters when accessing Chinese characters in C # and MySQL _ MySQL

Source: Internet
Author: User
This article mainly introduces how to avoid garbled characters when accessing Chinese characters in C # and MySQL. it is still the old method to convert to Unicode encoding first, for more information about how to use socket to develop network programs, you may receive and send Chinese characters in most cases, if the sent string is processed in the default method, a bunch of garbled characters will be generated.

Because Chinese characters are expressed in double bytes, the processing of strings containing Chinese characters must be in UNICODE encoding mode. that is to say, when sending a Chinese string using a socket, the string must be converted to the UNICODE format in advance.

The following is a simple socket communication code.

// Server code

Try {IPAddress MyIP = IPAddress. parse ("127.0.0.1"); TcpListener MyListener = new TcpListener (MyIP, Convert. toInt16 ("1235"); MyListener. start (); Socket MySocket = MyListener. acceptSocket (); byte [] MyData = new byte [256]; MySocket. receive (MyData); this. richTextBox1.AppendText (System. text. encoding. unicode. getString (MyData); UnicodeEncoding MyInfo = new UnicodeEncoding (); MySocket. send (MyInfo. getByte S ("From Server: Hello! I am a server ");} catch (Exception ex) {MessageBox. Show (ex. Message," Message prompt ", MessageBoxButtons. OK, MessageBoxIcon. Information );}

// Client code

Try {TcpClient MyClient = new TcpClient (); MyClient. connect ("127.0.0.1", Convert. toInt16 ("1235"); Stream MyStream = MyClient. getStream (); UnicodeEncoding MyInfo = new UnicodeEncoding (); byte [] MyData = new byte [256]; byte [] MySendByte = MyInfo. getBytes ("From Client: Hello! I am a client "); MyStream. write (MySendByte, 0, MySendByte. length); MyStream. read (MyData, 0,256); this. richTextBox1.AppendText (System. text. encoding. unicode. getString (MyData);} catch (Exception ex) {MessageBox. show (ex. message, "Message prompt", MessageBoxButtons. OK, MessageBoxIcon. information );}

In addition, Chinese access to the database is also a headache. In fact, it is very easy to solve this problem. The following is a piece of SQL Server code:

Try {SqlConnection MyConnect = new SqlConnection (m_ConnectionStrings); // insert a new record string MySQL = string. format ("insert into [BSM_SystemServiceInfoTable] VALUES ({0}, n' {1} ', n' {2}', n' {3 }', n' {4} ') ", AgentID, data [1], data [2], data [3], data [0]); MyCommand = new SqlCommand (MySQL, myConnect); MyCommand. connection. open (); MyCommand. executeNonQuery (); MyCommand. connection. close ();} catch (System. exception e) {MessageBox. show (e. toString ());}

We can see that in the SQL script command, all the string parameters have an additional character "N", which is declared to be encoded in UNICODE mode. of course, note that, if the field value may contain Chinese characters, the field type must be declared as nchar, nvarchar, and ntext. here, n indicates the same meaning.

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.