通过ASP.NET在MySql4中插入Unicode文本 [英] Insert unicode text in MySql4 throught ASP.NET

查看:76
本文介绍了通过ASP.NET在MySql4中插入Unicode文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为一个研究所编写一个新网站,该研究所有4000个用户存储在MySql4中!

I'm writing a new website for an institute which has 4000 users that are stored in MySql4 !

我正在使用mysql-connector/net通过asp.net连接到MySql数据库,

I'm using mysql-connector/net to connect to MySql database throught asp.net,

一切正常,除了更新unicode模式下的信息!

Everything is OK, EXCEPT updating the info which are in unicode mode !

不幸的是,我对Unicode知之甚少!

Unfortunately I don't know lot about unicode !

从MySql数据库连接和读取数据是这样的:

Connecting and reading data from MySql database is like this :

using MySql.Data.MySqlClient;
...
MySqlConnection con;
MySqlCommand cmd;

string tempstr = "Database=DB-NAME;Data Source=SOURCE;charset=utf8;User Id=USER;Password=PASS";
con = new MySqlConnection(tempstr);
con.Open();
cmd = con.CreateCommand();
cmd.CommandText = "select * from list_membership";
cmd.CommandType = CommandType.Text;
MySqlDataReader reader = cmd.ExecuteReader();
reader.Read();

例如,现在当我读取地址时,我会得到如下信息:زÙجاÙ

Now when I read an address for example I get sth like this: زÙجاÙ

幸运的是,我已经使用blew函数解决了这个问题:

Fortunately I've solved it using the function blew:

public string DecodeFromUtf8(string utf8String)
{
   // copy the string as UTF-8 bytes.
   byte[] utf8Bytes = new byte[utf8String.Length];
   for (int i = 0; i < utf8String.Length; ++i)
   {
       //Debug.Assert( 0 <= utf8String[i] && utf8String[i] <= 255, "the char must be in byte's range");
       utf8Bytes[i] = (byte)utf8String[i];
   }
   return Encoding.UTF8.GetString(utf8Bytes, 0, utf8Bytes.Length);
}

现在,我将垃圾字符串发送给函数,并得到如下正确的形式:زنجان(波斯语)

Now I send the junky string to the function and I get correct form like this : زنجان (persian language)

问题是,例如,当我想更新或插入地址字段时,除了像这样的问号之外,什么都不会存储在数据库中: ?????????

The problem is that when I want to update or insert address field for instance, nothing will store in the database except some question marks like this: ???? ?????

任何人都知道我该怎么办?!DecodeFromUtf8函数有任何反向形式吗?

Anyone know what should I do ?! Is there any reverse form for DecodeFromUtf8 function ?

不好意思,谢谢.

推荐答案

我不知道这是否对解决问题有帮助,但是 DecodeFromUtf8 的反应该看起来像这样:

I don't know if this helps with the problem, but the reverse of DecodeFromUtf8 should look something like this:

public string EncodeToMySQL4(string str)
{
   Encoding latin1 = new Encoding(1252);
   return latin1.GetString(Encoding.UTF8.GetBytes(str));
}

这篇关于通过ASP.NET在MySql4中插入Unicode文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆