寫入中文, 要先把中文轉成latin1編碼
Dim TmpStr as String = System.Text.Encoding.GetEncoding("latin1").GetString(System.Text.Encoding.Default.GetBytes("戀中國"))
再把latin1編碼的文字轉成二進制句子
Dim Tmp() As Byte = System.Text.Encoding.GetEncoding("latin1").GetBytes(TmpStr)
Dim i As Integer,nResult as String
nResult = "0x"
For i = 0 To Tmp.Length - 1
nResult = nResult & Format(Tmp(i), "x")
Next i
得到nResult = 0x91d9d6d087f8
這時使用sql陳述句 "INSERT INTO char_name__list SET old_char_name=0x91d9d6d087f8"
就可以存入正確中文了....
同樣,"SELECT xxxxx WHERE char_name=0x91d9d6d087f8"也一樣有效
注:如果不轉成二進制句子只用TmpStr,句子里有GBK繁體字符的話會變成問號。
要得到mysql的中文資料....要使用
cast(binary 欄名 as char character set gbk) as 欄名
例如我要得到 表格'pc'里的 'char_name' 欄里的中文資料,可以寫成
"SELECT cast(binary char_name as char character set gbk) as char_name FROM pc"
也可做個Function
Public Function GBK_Result(ByVal Source As String) As String
Return "cast(binary " & Source & " as char character set gbk) as " & Source
End Function
要用時 Sql="SELECT " & GBK_Result("Char_name") & " From pc"
這樣得到的'Char_name'欄資料就是GBK編碼的了
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/237374.html
標籤:VB.NET
