问题
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;"/>
回答1:
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);
and back to normal string by this
byte[] b = Convert.FromBase64String(strModified);
strOriginal = System.Text.Encoding.UTF8.GetString(b);
回答2:
Use this connection string
Datasource=localhost; Database=testdb; Uid=root; Pwd=123;CHARSET=utf8
And make sure it's CHARSET and not CharSet or charset!
Also try to change the DEFAULT CHARACTER SET of your tables and fields:
ALTER TABLE `myTbl` DEFAULT CHARACTER SET utf8
回答3:
I have faced the same problem.
I add the CHARSET=utf8 in my webconfig simply and solve the problem
<connectionStrings>
<add name="ConnectionString" connectionString="Server=192.168.1.4;Database=downtodate;Uid=root;CHARSET=utf8"/>
</connectionStrings>
回答4:
Sounds like you have an encoding issue. Make sure you are using UTF8 encoding across the board. For MySQL, that means making sure the table is UTF8, not latin1 or some other encoding. Also make sure your connection to MySQL is UTF8. I'm not sure how to do that in ASP/C#, but usually you can just run the "query" SET NAMES UTF8 after you connect and it will set things straight.
You can also do that query on the command line/prompt, which will stop the displaying of ???. Note that it won't convert already incorrectly stored data in the database, so existing data may or may not display properly.
回答5:
how to insert arabic into mysql from vb.net.
make sure that collection and character set have utf8 and utf8_unicode
add 'N' before unicode text
strconn = "server=servername;port=3306;database=database;uid=user;pwd=pass;Character Set=utf8;"
strsql = "UPDATE table SET ItemNameArabic= N'ArabicName' "
Dim conn As MySqlConnection = New MySqlConnection(strconn)
Dim cmd As MySqlCommand = New MySqlCommand()
cmd.CommandText = strsql
cmd.Connection = conn
conn.Open()
Try
Dim aff As Integer = cmd.ExecuteNonQuery()
MessageBox.Show(aff & " rows were affected.")
Catch
MessageBox.Show("Error encountered during INSERT operation.")
Finally
conn.Close()
End Try
回答6:
The problem is, yes your database charset is utf8, but your application does not know it. it think your charset as cp1256.
<add name="mysqlconnectionstring" connectionString="Data Source=localhost;Database=mydatabase_;Uid=root;Pwd=root;CharSet=cp1256;"/>
You should declare it. there is some solutions but easiest solution is add your connection string charset=utf8;
use it to solve problem
<add name="mysqlconnectionstring" connectionString="Data Source=localhost;Database=mydatabase_;Uid=root;Pwd=root;CHARSET=utf8;"/>
来源:https://stackoverflow.com/questions/13441787/how-to-insert-arabic-into-mysql-from-asp-net-c-sharp