You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.Net Core使用Dapper调用MariaDB存储过程提示参数数量错误问题

问题排查与解决方案

核心问题原因

你遇到的参数缺失报错,绝大多数是MySQL驱动参数前缀和参数名不匹配导致的,Dapper默认按你给定的参数名传递,但MariaDB/MySQL存储过程的入参不需要带@前缀,且多数MySQL驱动的默认参数前缀是?而非@,导致参数无法和存储过程的入参对应上,存储过程判定为没有传入参数。

具体修改步骤

  • 去掉参数名前的@前缀,或者在连接字符串里添加AllowUserVariables=True;允许用@作为参数前缀
  • 日期类型参数直接传DateTime对象,不要传字符串,避免类型转换异常
  • 确认参数方向设置为ParameterDirection.Input,和存储过程的IN入参对应

修正后的代码示例

DynamicParameters _parameters = new DynamicParameters();
// 去掉参数名前的@前缀
_parameters.Add("_entity_number", 111, DbType.Int32, ParameterDirection.Input, 11);
_parameters.Add("_price", 123.5501m, DbType.Decimal, ParameterDirection.Input);
// 直接传DateTime类型,不要传字符串
_parameters.Add("_valuation_date", new DateTime(2021,1,1), DbType.DateTime, ParameterDirection.Input);
// 确认存储过程名称的数据库前缀正确,commandType必须指定为StoredProcedure
_dapper.Execute("dbname.proc_EnterRow", _parameters, commandType: CommandType.StoredProcedure);

其他兼容问题处理

如果修改后还是报错,建议替换MySQL驱动为官方推荐的MySqlConnector,旧版MySql.Data驱动存在大量存储过程调用的兼容性问题。

内容的提问来源于stack exchange,提问作者Roddy Dairion

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.12 18:33:03