C# 在Visual Studio中使用dotConnect Postgresql时发生PgSqlParameter错误

C# 在Visual Studio中使用dotConnect Postgresql时发生PgSqlParameter错误,c#,postgresql,dotconnect,C#,Postgresql,Dotconnect,我有一个存储过程。输入为'id',输出为'n'。 但是,当我尝试在Visual Studio中运行它时,出现了一个错误:命令执行结果中缺少输出参数“n”的值 这是我的密码: int id = Convert.ToInt32(this.textBox1.Text); PgSqlConnection con = new PgSqlConnection(); con.ConnectionString = Properties.Settings.Default.DBConnectionString; P

我有一个存储过程。输入为'id',输出为'n'。 但是,当我尝试在Visual Studio中运行它时,出现了一个错误:命令执行结果中缺少输出参数“n”的值

这是我的密码:

int id = Convert.ToInt32(this.textBox1.Text);
PgSqlConnection con = new PgSqlConnection();
con.ConnectionString = Properties.Settings.Default.DBConnectionString;
PgSqlCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "getcountmaterials";
PgSqlParameter param = new PgSqlParameter("n", SqlDbType.Int);
param.Direction = ParameterDirection.Output;
cmd.Parameters.Add(param);
cmd.Parameters.Add(new PgSqlParameter("@id", id));
con.Open();
cmd.ExecuteNonQuery();
string kolvo = cmd.Parameters["n"].Value.ToString();
con.Close();
this.result.Text = kolvo;
存储过程:

CREATE OR REPLACE FUNCTION public.getcountmaterials(id integer)
  RETURNS integer AS
$BODY$
declare n integer;
begin n := (select sum(count) from materials_in_warehouses
 where id_materials = id);
return n;
end;
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;
ALTER FUNCTION public.getcountmaterials(integer)
  OWNER TO postgres;

我从未使用过Pg的dotConnect(不过,我是Oracle的超级粉丝),因此我无法验证我在所有这些方面的语法是否正确

也就是说,我想我看到了你的核心问题。Postgresql使函数和“存储过程”之间的界限有些模糊

您真正想做的就是在上面的函数上运行
选择
。因此,我相信以下措施会奏效。我知道这将适用于NpgSql,我希望它能正确地转换为dotConnect:

PgSqlCommand cmd = new PgSqlCommand("select getcountmaterials(:ID)", con);
cmd.Parameters.AddWithValue("ID", id);
string kolvo = cmd.ExecuteScalar().ToString();

我从来没有使用过Pg的dotConnect(不过,我是Oracle的超级粉丝),所以我无法验证我在所有这些方面的语法是否正确

也就是说,我想我看到了你的核心问题。Postgresql使函数和“存储过程”之间的界限有些模糊

您真正想做的就是在上面的函数上运行
选择
。因此,我相信以下措施会奏效。我知道这将适用于NpgSql,我希望它能正确地转换为dotConnect:

PgSqlCommand cmd = new PgSqlCommand("select getcountmaterials(:ID)", con);
cmd.Parameters.AddWithValue("ID", id);
string kolvo = cmd.ExecuteScalar().ToString();