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

调用PostgreSQL存储过程遇CLR类型与Int16Handler不匹配错误

问题排查与解决

错误"Can't write CLR type System.String with handler type Int16Handler"的核心原因是参数类型不匹配:你将CLR字符串类型的值传给了PostgreSQL中定义为smallint(对应CLR的short)类型的参数,Npgsql的Int16Handler无法处理字符串类型的输入。

结合你给出的参数值,问题出在makerID = "-1"这个参数上:存储过程s_pc_efo_operation.s_p_efo_pmdnumber中的makerID参数应为smallint类型,但你传入了字符串类型的"-1"。

解决步骤:

  1. 确认存储过程参数定义
    执行以下SQL查看存储过程的参数类型:

    SELECT proargnames, proargtypes FROM pg_proc 
    WHERE proname = 's_p_efo_pmdnumber' 
      AND pronamespace = (SELECT oid FROM pg_namespace WHERE nspname = 's_pc_efo_operation');
    

    确认makerID对应的类型是smallint(oid为21)或integer(oid为23)。

  2. 修正参数赋值与类型配置
    将字符串"-1"转换为对应数值类型,并确保Npgsql参数的类型与存储过程匹配:

    • 如果存储过程参数是smallint:
      if (!short.TryParse("-1", out short makerIdValue))
      {
          throw new ArgumentException("makerID值无法转换为short类型");
      }
      var makerIdParam = new NpgsqlParameter("makerID", NpgsqlDbType.Smallint)
      {
          Value = makerIdValue
      };
      
    • 如果存储过程参数是integer:
      if (!int.TryParse("-1", out int makerIdValue))
      {
          throw new ArgumentException("makerID值无法转换为int类型");
      }
      var makerIdParam = new NpgsqlParameter("makerID", NpgsqlDbType.Integer)
      {
          Value = makerIdValue
      };
      
  3. 检查其他参数的一致性
    顺带确认其他参数的类型配置:

    • Date参数应配置为NpgsqlDbType.Date或NpgsqlDbType.Timestamp,对应CLR的DateTime类型;
    • locationCode是int类型,需确认存储过程参数为integer,配置对应的NpgsqlDbType.Integer。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.31 21:30:55