Insert "????" in mysql database when post from c# code in Persian language

时间:2018-02-03 08:33:09

标签: c# mysql wordpress entity-framework

i have a problem when i insert a post in mysql database with mysql entity in c#. The persian letters in inserted record convert to "????" but when i read from database persian contents (that stored before) they're shown very well.

My table collation is UTF8mb4_unicode_520_ci and when I insert post from wordpress is shown well but when I try the same in my code I have the specified problem

Here is my code for inserting in database:

wp_posts post = new wp_posts
        {
            post_author = 1,
            post_date = DateTime.Now,
            post_date_gmt = DateTime.Now,
            post_content = "<h2>" + txtPersianContent.Text + "</h2>",
            post_title = txtPersianTitle.Text,
            post_excerpt = "None",
            post_status = "publish",
            comment_status = "open",
            ping_status = "open",
            post_password = "",
            post_name = txtName.Text,
            post_modified = DateTime.Now,
            post_modified_gmt = DateTime.Now,
            guid = txtGUID.Text,
            to_ping = "None",
            pinged = "None",
            post_content_filtered = "None",
            post_parent = 0,
            menu_order = 0,
            post_type = "post",
            post_mime_type = "",
            comment_count = 0
        };
db.wp_posts.Add(post);
db.SaveChangesAsync();

and picture from mysql

Please help me to fix this problem ;)

2 个答案:

答案 0 :(得分:3)

检查你的连接字符串!您应该使用与mysql的连接指定字符集:

Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword; CharSet=utf8;

答案 1 :(得分:0)

Before you do anything with non ascii characters, you should first tell your program (PHP, C#, whatever) which encoding it should expect when reading the input. You are probably using encoding that is different from your actual input which will "garble" the text.