带有c#后端的PostgreSQL发送了无法识别的响应类型:o

带有c#后端的PostgreSQL发送了无法识别的响应类型:o,c#,postgresql,C#,Postgresql,我正在尝试执行以下函数,如C#中的ExecuteOnQuery: 在我看来,Postgres在函数和存储过程之间的界限相当模糊。你现在所做的似乎在甲骨文中是可行的,而且据我所知,它在博士后中可能会很好地发挥作用 也就是说,在PostgreSQL中“执行”存储过程的方法是简单地选择函数。本着这种精神,这是您的代码的一个版本(经过一些重构——如果您讨厌它,请原谅我),我非常有信心它将执行: using (NpgsqlCommand command = new NpgsqlCommand( "

我正在尝试执行以下函数,如C#中的ExecuteOnQuery:


在我看来,Postgres在函数和存储过程之间的界限相当模糊。你现在所做的似乎在甲骨文中是可行的,而且据我所知,它在博士后中可能会很好地发挥作用

也就是说,在PostgreSQL中“执行”存储过程的方法是简单地选择函数。本着这种精神,这是您的代码的一个版本(经过一些重构——如果您讨厌它,请原谅我),我非常有信心它将执行:

using (NpgsqlCommand command = new NpgsqlCommand(
    "select Fifty(:CDE, :CPARA, :VDE, :VPARA, :IDP)", this.dllConexao.ConexaoBd))
{
    if (!dllConexao.open(Processadora.Comum.tipoConexao, 1, true))
    {
        clsRetorno.ret = 99;
        clsRetorno.msg = "Sistema temporariamente indisponível, " +
           "tente novamente mais tarde";
    }
    if (this.dllConexao.transactionIsOpen())
    {
        command.Transaction = this.dllConexao.TransacaoBd;
    }

    command.Parameters.Add("CDE", NpgsqlDbType.Bigint);
    command.Parameters.Add("CPARA", NpgsqlDbType.Bigint);
    command.Parameters.Add("VDE", NpgsqlDbType.Numeric);
    command.Parameters.Add("VPARA", NpgsqlDbType.Numeric);
    command.Parameters.Add("IDP", NpgsqlDbType.Bigint);

    command.Parameters[0].Value = cpfDe;
    command.Parameters[1].Value = cpfPara;
    command.Parameters[2].Value = valorDe;
    command.Parameters[3].Value = valorPara;
    command.Parameters[4].Value = idPagamento;

    command.ExecuteScalar();

    this.dllConexao.commitTransacao();
    this.dllConexao.closeTransaction();
}
我故意更改了参数的名称,使其与函数中参数的名称不匹配。这是因为这无关紧要。只要您的SQL使用
@
列出了参数的名称,并且这些名称对应于您命名的
NpgSqlParameter
值,您就可以开始了

如果您的数据类型完全匹配(long、long、decimal、decimal、long),您甚至可以进一步重构这一步骤:

command.Parameters.AddWithValue("CDE", cpfDe);
command.Parameters.AddWithValue("CPARA", cpfPara);
command.Parameters.AddWithValue("VDE", valorDe);
command.Parameters.AddWithValue("VPARA", valorPara);
command.Parameters.AddWithValue("IDP", idPagamento);

另一个很好的地方是,如果您确实有异常,我相信错误消息可能会非常有用。

用C语言显示代码怎么样?这就是所谓的?抱歉@Steve,我确定我粘贴了代码,不管怎样!嗯,不确定它是否有任何区别,但最后一个参数是Int32,而函数需要一个bigint@Steve,实际上,这将错误更改为:后端发送了无法识别的响应类型:\是的,这正是我需要的,一条更好的错误消息,并看到它工作!令人惊叹的。谢谢
using (NpgsqlCommand command = new NpgsqlCommand(
    "select Fifty(:CDE, :CPARA, :VDE, :VPARA, :IDP)", this.dllConexao.ConexaoBd))
{
    if (!dllConexao.open(Processadora.Comum.tipoConexao, 1, true))
    {
        clsRetorno.ret = 99;
        clsRetorno.msg = "Sistema temporariamente indisponível, " +
           "tente novamente mais tarde";
    }
    if (this.dllConexao.transactionIsOpen())
    {
        command.Transaction = this.dllConexao.TransacaoBd;
    }

    command.Parameters.Add("CDE", NpgsqlDbType.Bigint);
    command.Parameters.Add("CPARA", NpgsqlDbType.Bigint);
    command.Parameters.Add("VDE", NpgsqlDbType.Numeric);
    command.Parameters.Add("VPARA", NpgsqlDbType.Numeric);
    command.Parameters.Add("IDP", NpgsqlDbType.Bigint);

    command.Parameters[0].Value = cpfDe;
    command.Parameters[1].Value = cpfPara;
    command.Parameters[2].Value = valorDe;
    command.Parameters[3].Value = valorPara;
    command.Parameters[4].Value = idPagamento;

    command.ExecuteScalar();

    this.dllConexao.commitTransacao();
    this.dllConexao.closeTransaction();
}
command.Parameters.AddWithValue("CDE", cpfDe);
command.Parameters.AddWithValue("CPARA", cpfPara);
command.Parameters.AddWithValue("VDE", valorDe);
command.Parameters.AddWithValue("VPARA", valorPara);
command.Parameters.AddWithValue("IDP", idPagamento);