displaying chinese character to vb.net from mysql database

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cheche08
    New Member
    • Jul 2013
    • 2

    displaying chinese character to vb.net from mysql database

    I am using vb.net and MySql database.

    After storing Chinese character to MySql, I can't retrieve that Chinese character to vb.net?

    How is it possible to retrieve Chinese character from MySql to vb.net TextBox?

    Thank you so much for your response.
    Last edited by Frinavale; Jul 15 '13, 02:12 PM. Reason: Fixed grammar.
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    You need to specify the Character Encoding to use.

    If you are not using the correct encoding, you will not see the right characters displayed.

    Along with the MSDN article link that I provided you above, you might also be interested in this article Using Character Encoding in ASP.NET.

    -Frinny
    Last edited by Frinavale; Jul 22 '13, 02:24 PM.

    Comment

    • cheche08
      New Member
      • Jul 2013
      • 2

      #3
      Thank you!
      I am using utf8_unicode_ci for the mysql database but i have difficulties on retrieving it on vb.net. When I tried to use convert to hex then byte to string the output is not the same.
      what i mean is it is not the output that i expect to display.

      here's my code:
      Code:
      Imports System.Data.Odbc
      Imports System.Data.Odbc.OdbcDataReader
      Imports System.Text
      Imports System.Globalization
      
      Public Class chuchu
      
          Private conn As New OdbcConnection("DSN=sample")
          Dim cmd As OdbcCommand
      
          Public che, chee, cheche As String
      
          Public Sub New()
      
              conn.Open()
              cmd = New OdbcCommand("SELECT * from sample1", conn)
      
          End Sub
      
          Private Function HexAsciiConvert(ByVal hex As String) As String
              Dim sb As New StringBuilder()
              For i As Integer = 0 To hex.Length - 2 Step 2
                  sb.Append(Convert.ToString(Convert.ToChar(Int32.Parse(hex.Substring(i, 2), System.Globalization.NumberStyles.HexNumber))))
              Next
              Return sb.ToString()
          End Function
      
      
      
          Public Sub data()
      
              'cmd.CommandText = "SELECT details FROM sample1 WHERE sampleID = '1'"
              cmd.CommandText = "select hex(details) from sample1 WHERE sampleID = '1'"
      
      
              Dim dr = cmd.ExecuteReader(che)
              If (dr.HasRows) Then
      
                  If (dr.Read()) Then
      
      
                      Dim uni = Encoding.Unicode
                      Dim charBytes As Byte() = Encoding.UTF8.GetBytes(dr.GetString(0))
                      Dim uniBytes As Byte() = Encoding.Convert(Encoding.UTF8, uni, charBytes)
                      che = uni.GetString(uniBytes)
      
                      che = HexAsciiConvert(dr.GetString(0))
      
                  End If
      
              End If
      
          End Sub
      Last edited by Frinavale; Jul 22 '13, 02:23 PM. Reason: Please use code tags when posting code.

      Comment

      Working...