【问题标题】:Insert "????" in mysql database when post from c# code in Persian language插入 ”????”从波斯语的 c# 代码发布时在 mysql 数据库中
【发布时间】:2018-02-03 08:33:09
【问题描述】:

当我在 c# 中使用 mysql 实体在 mysql 数据库中插入帖子时遇到问题。 插入记录中的波斯字母转换为“??????”但是当我从数据库中读取波斯语内容(之前存储的)时,它们显示得很好。

我的表格排序规则是 UTF8mb4_unicode_520_ci,当我从 wordpress 插入帖子时显示良好,但是当我在代码中尝试相同时,我遇到了指定的问题

这是我插入数据库的代码:

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

请帮我解决这个问题;)

【问题讨论】:

  • 对我来说听起来像是一个编码问题。您的 C# 代码是否使用与表相同的编码 (UTF8mb4_unicode_520_ci)?
  • 不是我的 c# 代码是字符串,因为它只是插入字符串
  • 尝试调试db.wp_posts.Add(post); 行。 post.post_content 包含什么? txtPersianContent.Text 包含什么?
  • @Sklivvz txtPersianContent.Text = "سلام به شما کاربران عزیز"
  • 另一个呢?

标签: c# mysql wordpress entity-framework


【解决方案1】:

检查您的连接字符串!您应该在连接到 mysql 时指定字符集:

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

【讨论】:

    【解决方案2】:

    在您对非 ascii 字符进行任何操作之前,您应该首先告诉您的程序(PHP、C# 等)在读取输入时应该使用哪种编码。您可能使用的编码与您的实际输入不同,这会使文本“乱码”。

    【讨论】:

    • 我使用字符串来获取或设置帖子
    • 一个字符串总是需要先知道编码,然后再读取它。如果该编码确实正确,则您可能需要将其转换为数据库的相应编码。
    • 另外,MySql 可能未配置为接受 UTF8。如果在插入记录之前使用查询“SET NAMES utf8”会发生什么?
    猜你喜欢
    • 2019-06-20
    • 2015-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-15
    • 2016-01-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多