C# 如何在C中使用Npgsql 4.1.5.0中的cursor INOUT参数调用PostgreSQL 13存储过程(无函数)#
我有一个带有IN-char参数和INOUT-cursor参数的存储过程:C# 如何在C中使用Npgsql 4.1.5.0中的cursor INOUT参数调用PostgreSQL 13存储过程(无函数)#,c#,npgsql,postgresql-13,C#,Npgsql,Postgresql 13,我有一个带有IN-char参数和INOUT-cursor参数的存储过程: CREATE OR REPLACE PROCEDURE SP_ObtenerFacturaPorNombreCliente(IN p_nombreCliente CHAR, INOUT p_cursorFacturas REFCURSOR) LANGUAGE PLPGSQL AS $$ BEGIN OPEN p_cursorFacturas FOR SELECT "CodigoFactura&
CREATE OR REPLACE PROCEDURE SP_ObtenerFacturaPorNombreCliente(IN p_nombreCliente CHAR, INOUT p_cursorFacturas REFCURSOR)
LANGUAGE PLPGSQL
AS
$$
BEGIN
OPEN p_cursorFacturas FOR
SELECT "CodigoFactura", "NombreCliente", "DireccionCliente", "TelefonoCliente", "Fecha", "SubTotal", "Iva", "ValorIva", "Total", "Geografico", "Geometrico" FROM "Factura"
WHERE "NombreCliente" = p_nombreCliente
ORDER BY "CodigoFactura";
END
$$
在C#中使用Npgsql 4.1.5.0调用存储过程:
带有:npgsqlParameter2.Value=string.Empty代码>我有以下错误:
42601:处或附近的语法错误
带有:npgsqlParameter2.Value=null代码>我有以下错误:
参数:必须设置p_cursorFacturas
更新:
通过@madreflection suggestion,我通过DBNull.Value
更改了null
,但调用的npgsqlParameter2
更改为“”
这回答了你的问题吗?尽管该问题/答案与SQL Server有关,但它几乎普遍适用于ADO.NET提供程序,包括Npgsql。这是否回答了您的问题@madreflection,使用npgsqlParameter2.Value=DBNull.Value;和npgsqlCommand.ExecuteNonQuery();我可以调用存储过程,但npgsqlParameter2更新为:“”,我如何才能获得光标值?@CaiusJard:我说得太快了。我刚刚发现,我已经用“proc_2;”命名了该对象,但将其重新创建为一个函数“临时”以尝试某些操作,并且忘记在搁置工作之前将其更改回去。Npgsql无法通过输出参数读取游标。@Ejrr1085:从函数返回时使用读取游标。尝试将存储过程包装到函数中,并使用新的命令对象执行fetchall-in
语句。这是否回答了您的问题?尽管该问题/答案与SQL Server有关,但它几乎普遍适用于ADO.NET提供程序,包括Npgsql。这是否回答了您的问题@madreflection,使用npgsqlParameter2.Value=DBNull.Value;和npgsqlCommand.ExecuteNonQuery();我可以调用存储过程,但npgsqlParameter2更新为:“”,我如何才能获得光标值?@CaiusJard:我说得太快了。我刚刚发现,我已经用“proc_2;”命名了该对象,但将其重新创建为一个函数“临时”以尝试某些操作,并且忘记在搁置工作之前将其更改回去。Npgsql无法通过输出参数读取游标。@Ejrr1085:从函数返回时使用读取游标。尝试将存储过程包装在函数中,并使用新的命令对象执行fetchall-in
语句。
NpgsqlConnection npgsqlConnection = new NpgsqlConnection("Server=127.0.0.1;Port=5432;Database=mybase;User Id=user;Password=password;");
npgsqlConnection.Open();
npgsqlConnection.TypeMapper.UseNetTopologySuite();
string sentencialSQL = "CALL SP_ObtenerFacturaPorNombreCliente(:p_nombreCliente, :p_cursorFacturas);";
NpgsqlCommand npgsqlCommand = new NpgsqlCommand(sentencialSQL, npgsqlConnection);
// ===============================
NpgsqlParameter npgsqlParameter1 = new NpgsqlParameter();
npgsqlParameter1.ParameterName = ":p_nombreCliente";
npgsqlParameter1.Value = "Perico de los palotes";
npgsqlParameter1.NpgsqlDbType = NpgsqlTypes.NpgsqlDbType.Text;
npgsqlParameter1.Direction = ParameterDirection.Input;
npgsqlCommand.Parameters.Add(npgsqlParameter1);
// -------------------
NpgsqlParameter npgsqlParameter2 = new NpgsqlParameter();
npgsqlParameter2.ParameterName = ":p_cursorFacturas";
npgsqlParameter2.Value = string.Empty;
npgsqlParameter2.NpgsqlDbType = NpgsqlTypes.NpgsqlDbType.Refcursor;
npgsqlParameter2.Direction = ParameterDirection.InputOutput;
npgsqlCommand.Parameters.Add(npgsqlParameter2);
// ===============================
npgsqlCommand.CommandType = CommandType.Text; // CommandType.StoredProcedure is with Function
NpgsqlDataReader npgsqlDataReader = npgsqlCommand.ExecuteReader();
NpgsqlConnection npgsqlConnection = new NpgsqlConnection("Server=127.0.0.1;Port=5432;Database=mybase;User Id=user;Password=password;");
npgsqlConnection.Open();
npgsqlConnection.TypeMapper.UseNetTopologySuite();
string sentencialSQL = "CALL SP_ObtenerFacturaPorNombreCliente(:p_nombreCliente, :p_cursorFacturas);";
NpgsqlCommand npgsqlCommand = new NpgsqlCommand(sentencialSQL, npgsqlConnection);
// ===============================
NpgsqlParameter npgsqlParameter1 = new NpgsqlParameter();
npgsqlParameter1.ParameterName = ":p_nombreCliente";
npgsqlParameter1.Value = "Perico de los palotes";
npgsqlParameter1.NpgsqlDbType = NpgsqlTypes.NpgsqlDbType.Text;
npgsqlParameter1.Direction = ParameterDirection.Input;
npgsqlCommand.Parameters.Add(npgsqlParameter1);
// -------------------
NpgsqlParameter npgsqlParameter2 = new NpgsqlParameter();
npgsqlParameter2.ParameterName = ":p_cursorFacturas";
npgsqlParameter2.Value = DBNull.Value;
npgsqlParameter2.NpgsqlDbType = NpgsqlTypes.NpgsqlDbType.Refcursor;
npgsqlParameter2.Direction = ParameterDirection.InputOutput;
npgsqlCommand.Parameters.Add(npgsqlParameter2);
// ===============================
npgsqlCommand.CommandType = CommandType.Text; // CommandType.StoredProcedure is with Function
NpgsqlDataReader npgsqlDataReader = npgsqlCommand.ExecuteReader();