一、报错说明
C# 调用存储过程中出现如下报错:
1、Incorrect string value: '\xE2\x84\xA2\xE4\xB9\x90...' for column 'gbkName' at row 1
在 MyEntity.ExecuteStoredProcedure(String procedureName, List`1& values) 位置 ..\RSSEntity.cs:行号 116
在 ModelsCode.StoredProcedure.AddUser(String& gbkName, Int32& Newid) 位置 ..\StoredProcedure.cs:行号 72
在 MyController.SyncUser(syncUserRequest requestData) 位置 ..\Controllers\MyController.cs:行号 85
二、背景介绍
2.1 字符集
- 数据库
Mysql
字符集:gbk
- 表
user_info
的字符集:gbk_chinese_ci
- 字段
user_name
字符集:utf8_general_ci
数据库的字符集
SHOW VARIABLES LIKE 'character_set_database';
Variable_name | Value |
---|---|
character_set_database | gbk |
表的字符集
- SELECT * FROM information_schema.`TABLES`
SELECT TABLE_COLLATION FROM information_schema.`TABLES` WHERE TABLE_NAME = 'user_info';
TABLE_COLLATION |
---|
gbk_chinese_ci |
gbk_chinese_ci |
- SHOW TABLE STATUS
SHOW TABLE STATUS WHERE Name = 'user_info';
在结果中,Collation
列表示表的字符集。
Name | Engine | Version | Row_format | Rows | Avg_row_length | Data_length | Max_data_length | Index_length | Data_free | Auto_increment | Create_time | Update_time | Check_time | Collation \fcolorbox{#D9EBF9}{#D9EBF9}{\color{#000000}{Collation}} Collation | Checksum | Create_options | Comment |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
user_info | InnoDB | 10 | Compact | 34 | 481 | 16384 | 0 | 16384 | 10485760 | 182 | 2024-10-08 09:25:09 | ( N u l l ) \textcolor{#B4BCD0}{(Null)} (Null) | ( N u l l ) \textcolor{#B4BCD0}{(Null)} (Null) | gbk_chinese_ci \fcolorbox{#0078D7}{#0078D7}{\color{#FFFFFF}{gbk\_chinese\_ci}} gbk_chinese_ci | ( N u l l ) \textcolor{#B4BCD0}{(Null)} (Null) |
列的字符集
SHOW FULL COLUMNS FROM user_info WHERE Field='user_name';
Field | Type | Collation \fcolorbox{#D9EBF9}{#D9EBF9}{\color{#000000}{Collation}} Collation | Null | Key | Default | Extra | Privileges | Comment |
---|---|---|---|---|---|---|---|---|
user_name | varchar(50) | utf8_general_ci \fcolorbox{#0078D7}{#0078D7}{\color{#FFFFFF}{utf8\_general\_ci}} utf8_general_ci | NO | ( N u l l ) \textcolor{#B4BCD0}{(Null)} (Null) | select,insert,update,references | 用户名 |
2.2 存储过程AddUser(未改造前)
2.2.2 参数 IN gbkName VARCHAR(50),OUT Newid INT \fcolorbox{#787878}{#FFFFFF}{\color{#000000}{IN gbkName VARCHAR(50),OUT Newid INT}} IN gbkName VARCHAR(50),OUT Newid INT
2.2.2 返回类型 | \fcolorbox{#CCCCCC}{#F0F0F0}{\color{#000000}{|\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space }} |
2.2.1 类型 PROCEDURE ﹀ \fcolorbox{#ADADAD}{#E1E1E1}{\color{#000000}{PROCEDURE\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space ﹀}} PROCEDURE ﹀
2.2.3 定义
BEGIN
INSERT INTO user_info(user_name)VALUES(gbkName);
SELECT last_insert_id() INTO userId;
SET Newid = userId;
END
三、解决方案
修改存储过程AddUser
3.1 修改参数 IN gbkName VARCHAR(50) CHARACTER SET utf8,OUT Newid INT \fcolorbox{#787878}{#FFFFFF}{\color{#000000}{IN gbkName VARCHAR(50) CHARACTER SET utf8,OUT Newid INT}} IN gbkName VARCHAR(50) CHARACTER SET utf8,OUT Newid INT
3.2 修改定义
BEGIN
DECLARE utf8Name VARCHAR(50) CHARACTER SET utf8;
SET utf8Name = CONVERT(gbkName USING utf8);
-- ...
INSERT INTO user_info(user_name)VALUES(utf8Name);
SELECT last_insert_id() INTO userId;
SET Newid = userId;
END