c# - 使用 .NET C# MySQL 插入表情符号

标签 c# mysql .net encoding emoji

我正在尝试将表情符号插入我的 mysql。但它显示为😋 ->“??”。

这就是我到目前为止所做的。

我的 ASPX 页面

<meta charset="utf-8">

我的数据库表设置为:

 utf8mb4_unicode_ci

我的数据库列设置为:

 utf8mb4_unicode_ci

我的 MySQL 连接字符串:

server=mysql.server.com;uid=testuser;pwd=1234;database=testdb;convert zero datetime=True;charset=utf8mb4;

但是,如果我直接在 phpMyAdmin 中使用 sql 语句插入表情符号,它会完美运行

INSERT INTO Notification (id, headline, notificationText, sentDate) 
VALUES (null, 'test', '😋', NOW())

但是当我尝试通过代码(.NET C#)插入时,它显示为“??”。

private void EmojiQueryTester()
{
    string strSql = "INSERT INTO Notification (id, headline, notificationText, sentDate) " + 
                     " VALUES (null, 'test', '😋', NOW())";
    string strConnectionString = "mysql.server.com;uid=testuser;pwd=1234;database=testdb;" + 
                                 "convert zero datetime=True;charset=utf8mb4";

    using (var mySqlConnection = new MySqlConnection(strConnectionString))
    {
        mySqlConnection.Open();
        var mySqlCommand = new MySqlCommand(strSql, mySqlConnection);
        mySqlCommand.ExecuteNonQuery();
    }
}

enter image description here

id = 27 由我的 .NET 代码插入,id = 28 由 phpMyAdmin 插入

我也尝试过插入其他字符,如下所示,但仍然没有成功:

U+1F601    |    \xF0\x9F\x98\x81     | 

显示创建表通知

CREATE TABLE `Notification` (
  `Id` int(11) NOT NULL AUTO_INCREMENT,
  `headline` varchar(255) COLLATE utf8mb4_bin NOT NULL,
  `notificationText` mediumtext COLLATE utf8mb4_unicode_ci NOT NULL,
  `sentDate` date NOT NULL,
  PRIMARY KEY (`Id`)
) ENGINE=InnoDB AUTO_INCREMENT=40 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci

enter image description here

SELECT notificationText, HEX(notificationText) FROM Notification

这是我的编码查询

SHOW VARIABLES WHERE Variable_name LIKE 'character\_set\_%' OR Variable_name LIKE 'collation%' 

enter image description here

打印出十六进制字符串

public static string ConvertStringToHex(String input, System.Text.Encoding encoding)
{
    Byte[] stringBytes = encoding.GetBytes(input);
    StringBuilder sbBytes = new StringBuilder(stringBytes.Length * 2);
    foreach (byte b in stringBytes)
    {
        sbBytes.AppendFormat("{0:X2}", b);
    }
    return sbBytes.ToString();
}

protected void Page_Load(object sender, EventArgs e)
{
    string notification = "😀";

    DBTool dbT = new DBTool();
    dbT.tester(notification);

    Response.Write(ConvertStringToHex(notification, Encoding.UTF8));
}

浏览器中的输出:F09F9880

dbo.HEX(通知):3F3F

dbo.通知:??

因此浏览器中的输出基本上是笑脸的正确十六进制,但是在 dbo.HEX(notification) 中它会转换为“3F3F”。所以基本上它是用 ASCII 编写的,而不是应该用 UTF8 编写的。

最佳答案

将其放入连接字符串中:

id=my_user;password=my_password;database=some_db123;charset=utf8mb4;

Console.OutputEncoding = System.Text.Encoding.UTF8;

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

并确保表中的列是字符集utf8mb4

为了进一步调试:

HEX('😋') in utf8mb4 is these 4 bytes: F09F988B
in utf16:  D83DDE0B

关于c# - 使用 .NET C# MySQL 插入表情符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59785129/

相关文章:

c# - 双显示器屏幕捕获问题

mysql - 如何防止两个引用共享同一个主键

java - 为什么当我在 MySQL 数据库中的不同列上使用 "no default value"时,会在单独的列上收到 "INSERT INTO"错误?

.net - 在 Windows (.Net) 上转换 JPEG 色彩空间(Adobe RGB 到 sRGB)

c# - 以最小的延迟快速显示非常高分辨率图像的缩略图

c# - System.Single 精度与 C#

c# - 如何使用window.open在mvc中的jquery中的同一窗口中打开 Controller 操作?

c# - ASP.NET 核心 : calling a view component does not respect authorization attribute

c# - 哪种布局 if() { try {} catch {} } 更好?

c# - 动态查询 SQL Server