Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What about Chinese garbled codes when Dapper operates MySQL database to obtain JSON data?

2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "how to do Chinese garbled code when Dapper operates MySQL database to obtain JSON data". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

In the project, the JSON data is stored in the MySQL database by Dapper, and the Chinese garbled code in the JSON data is found.

In order to find out what the final problem is, let's start all over again. first, we give the following test entities and the tables in the database, as follows:

For the convenience of data operation, we operate through the package [Dapper.SimpleCRUD]. Insert data and query data as follows:

If all is normal above, next we will change the column type of JSON data to json, and then do so. As a result, we will find that the data will be garbled in Chinese:

At first, I wondered if there was something wrong with the mapping of the package [Dapper.SimpleCRUD], so I used Dapper to make the following query, and there was still garbled code in Chinese as above:

Therefore, we can basically draw a conclusion from the above: for the type json in MySQL, if there is Chinese in the json data, there will be garbled codes when querying with Dapper. So how to solve this problem? Therefore, when adding data, I encode the Chinese with URL, and then decode it when querying, as follows:

Of course, the above solution is solved by the actual project business, and our project needs to obtain the Chinese in the JSON data and then transmit it through URL, eliminating the need to request the interface to get the stored name. So far we have learned that Dapper for json data types will lead to the problem of garbled characters in queries, but what is the root cause? The above driver for connecting to MySQL uses the driver officially provided by Oracle and is the latest version, as follows:

We know that there are a lot of problems when operating MySQL database through EF Core, so most of them use the package [Pomelo.EntityFrameworkCore.MySql], because it contains [MySqlConnector], in other words, there is a driver connection for MySQL, so here we try to use this package to replace the above [MySql.Data] package and find that the Chinese is no longer garbled.

To sum up, we need to note that if we use the official driver package [MySql.Data], when the column type in MySQL is json, and if there is Chinese in the json data, Chinese garbled will occur when querying with Dapper, which needs to be converted. The above is set to utf8mb4 in the database connection string, so in theory it should have nothing to do with encoding.

This is the end of the content of "how to do Chinese garbled code when Dapper operates MySQL database to obtain JSON data". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report