如何从asp.net C#将阿拉伯语插入mysql [英] how to insert arabic into mysql from asp.net C#

查看:11
本文介绍了如何从asp.net C#将阿拉伯语插入mysql的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从asp.net页面插入阿拉伯语内容到mysql5.5,我使用mysqlconnector5.2.7连接本地mysqlserver.当我试图从asp.net程序插入阿拉伯语时,空值被插入到数据库中,尽管当我尝试从命令提示符插入时,所有单词都显示为 ???,我在 google 上搜索了更多内容,但找不到任何解决方案.请参阅下面的代码.我更改了数据库、表格和使用命令提示符下的更改查询将阿拉伯语列转换为 utf8,但我仍然找不到任何解决方案,如何在 datagridview 上显示此阿拉伯语

I want to insert arabic content from asp.net page to mysql5.5,I am using mysqlconnector5.2.7 to connect with local mysqlserver.when i tried to insert arabic from asp.net program,null values are inserted into the database,despite of it when i tried to insert from command prompt,all words are showing like ???,I searched more on google,but i could not found any solution.Please see my code below.I altered the database,table and the column of arabic to utf8 using the alter query on command prompt,but still i cannot find any solution,how to display this arabic on datagridview

myconnection.Open();
string insert="insert into home_table(image,content,arabiccontent) values(@image,@content,arabiccontent)";
MySqlCommand cmd = new MySqlCommand(insert,myconnection);
MySqlParameter param = new MySqlParameter("@image", MySqlDbType.VarBinary, image.Length);
param.Value = image;
cmd.Parameters.Add(param);

cmd.Parameters.Add(new MySqlParameter("@content",MySqlDbType.VarChar,Content.Length));

cmd.Parameters["@content"].Value = Content;
cmd.Parameters.Add(new MySqlParameter("@arabiccontent", MySqlDbType.VarChar, contentarabic.Length));
cmd.Parameters["@arabiccontent"].Value = contentarabic.Trim();
cmd.ExecuteNonQuery();
myconnection.Close();

web.config

 <add name="mysqlconnectionstring" connectionString="Data Source=localhost;Database=mydatabase_;Uid=root;Pwd=root;CharSet=cp1256;"/>

推荐答案

最好的方法是将您的文本转换为 base 64 字符,然后将其存储到数据库中.而从数据库中获取它时使用反之亦然转换.

The best way is to convert your text into base 64 character and then store it to database. And while fetching it from database use vice-versa conversion.

 byte[] byt = System.Text.Encoding.UTF8.GetBytes(strOriginal);
// convert the byte array to a Base64 string
strModified = Convert.ToBase64String(byt);

并由此恢复正常字符串

  byte[] b = Convert.FromBase64String(strModified);
strOriginal = System.Text.Encoding.UTF8.GetString(b);

这篇关于如何从asp.net C#将阿拉伯语插入mysql的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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