簡體   English   中英

使用 .NET C# MySQL 插入表情符號

[英]Insert emoji using .NET C# MySQL

我試圖將表情符號插入我的 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();
    }
}

在此處輸入圖片說明

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

在此處輸入圖片說明

SELECT notificationText, HEX(notificationText) FROM Notification

這是我的編碼查詢

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

在此處輸入圖片說明

打印出十六進制字符串

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.notification: ??

所以瀏覽器中的輸出基本上是笑臉的相應十六進制,但是在 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;

並確保表中的列是CHARACTER SET utf8mb4

進一步調試:

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

我的猜測是您的 .NET 應用程序具有 UTF-16 編碼。 他們倆 ?? 對應於 2 個字節(16 位)。

嘗試用 UTF-8 編碼您的字符串:

string sql = "... VALUES (null, 'test', '😋', NOW())";
byte [] bytes = Encoding.Default.GetBytes(sql);
string encoded = Encoding.UTF8.GetString(bytes);
// ...
var mySqlCommand = new MySqlCommand(encoded, mySqlConnection);

我不是 100% 確定,但是您向我們展示的所有其他內容似乎都是一致的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM