【Mysql】Incorrect string value: ‘\xE2\x84\xA2\xE4\xB9\x90...‘ for column ‘gbkName‘ at row 1

一、报错说明

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_nameValue
character_set_databasegbk

表的字符集

  1. 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
  1. SHOW TABLE STATUS
SHOW TABLE STATUS WHERE Name = 'user_info';

在结果中,Collation列表示表的字符集。


NameEngineVersionRow_formatRowsAvg_row_lengthData_lengthMax_data_lengthIndex_lengthData_freeAuto_incrementCreate_timeUpdate_timeCheck_time Collation \fcolorbox{#D9EBF9}{#D9EBF9}{\color{#000000}{Collation}} CollationChecksumCreate_optionsComment
user_infoInnoDB10Compact3448116384016384104857601822024-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';

FieldType Collation \fcolorbox{#D9EBF9}{#D9EBF9}{\color{#000000}{Collation}} CollationNullKeyDefaultExtraPrivilegesComment
user_namevarchar(50) utf8_general_ci \fcolorbox{#0078D7}{#0078D7}{\color{#FFFFFF}{utf8\_general\_ci}} utf8_general_ciNO ( 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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值