【问题标题】:Getting error No function matches the given name and argument types. You might need to add explicit type casts."出现错误没有函数匹配给定的名称和参数类型。您可能需要添加显式类型转换。”
【发布时间】:2019-01-25 13:22:50
【问题描述】:

我正在使用 Npgsql ADO 点网连接器从 C# 代码与 postgresql 进行通信。

该函数在 postgresql 数据库中可用,我也可以从 pgadmin 工具执行它,但无法从 c# 代码调用它。

遇到错误

42883:函数 public.insert_json_array_to_test_method_temp(p_input_test_name => 文本,p_input_test_type_xref => 文本,p_input_unit_Attribute => 文本) 不存在”}

错误提示 - 没有函数匹配给定的名称和参数类型。 您可能需要添加显式类型转换。

下面是C#代码

NpgsqlConnection conn = new NpgsqlConnection(_connStr);               
conn.Open();               
NpgsqlCommand cmd = new NpgsqlCommand("public.insert_json_array_to_test_method_temp", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new NpgsqlParameter("p_input_test_name", NpgsqlDbType.Text) { Value = testmethod });
cmd.Parameters.Add(new NpgsqlParameter("p_input_test_type_xref", NpgsqlDbType.Text) { Value = testtypexref });
cmd.Parameters.Add(new NpgsqlParameter("p_input_unit_Attribute", NpgsqlDbType.Text) { Value = unitattributes });

NpgsqlDataReader dr = cmd.ExecuteReader();

   while (dr.Read())
            {

            }         conn.Close();

下面是函数头部分

    CREATE OR REPLACE FUNCTION 
   public.insert_json_array_to_test_method_temp(p_input_test_name 
   text,p_input_test_type_xref text,p_input_unit_attribute text)
   RETURNS text
   LANGUAGE 'plpgsql'

     COST 100
VOLATILE 
 AS $BODY$  
 DECLARE res Text;

BEGIN

--some logic with parameters

res:= public."InsertTestNames"();

 Return 'done';

 END;

 $BODY$;

谁能帮忙解决一下这里的问题?

【问题讨论】:

  • 这真的不是您创建 postgresql 函数的方式。我建议查看手册 - postgresql.org/docs/current/sql-createfunction.html 此外,函数应该返回数据或执行任何操作。你的功能是做什么的?在我看来,这更像是一个创建表语句。
  • Georgi Raychev 我没有在这里粘贴完整的函数代码,函数有返回类型主体等。我也可以从 pgadmin 工具调用这个方法
  • 设置命令的时候需要public吗?
  • 也试过不公开。
  • 您的 postgresql 函数有问题,您没有向我们展示。你不能指望在这方面有太多帮助。可能你给它输入了错误的数据类型,但谁知道......

标签: c# postgresql npgsql


【解决方案1】:

我更改了创建参数的方式并进行了转换 - 现在可以正常工作了。

var parameter1 = cmd.CreateParameter();
parameter1.ParameterName = "parameter_name";
parameter1.NpgsqlDbType = NpgsqlDbType.Text;
parameter1.Value = parameter_value;
cmd.Parameters.Add(parameter1);

【讨论】:

    猜你喜欢
    • 2021-02-05
    • 1970-01-01
    • 2020-08-26
    • 1970-01-01
    • 1970-01-01
    • 2016-01-18
    • 1970-01-01
    • 2019-03-23
    • 1970-01-01
    相关资源
    最近更新 更多