how to insert arabic into mysql from asp.net C#

烈酒焚心 提交于 2019-12-06 12:58:09

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);

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

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>

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.

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

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;"/>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!