PostgreSQL 11创建或替换过程GetMultipleResultSets(INOUT ref1 refcursor,INOUT ref2 refcursor);使用Npgsql自动进行游标引用

问题描述 投票:0回答:1

要求:如何使用ado.net上的npgsql 4.0从postgresql 11过程(而不是函数)中获取多个refcursor数据而不使用fetch语句。

这里是我尝试过的样品:

Postgresql过程:

CREATE OR REPLACE PROCEDURE public.GetMultipleResultSets(
    INOUT ref1 refcursor,
    INOUT ref2 refcursor)
LANGUAGE 'plpgsql'

AS $BODY$
begin
    open ref1 for
    select * from public."tblTestTable1";

    open ref2 for
    select * from public."tblTestTable2";   
end;
$BODY$;

使用Npgsql 4.0的C#代码:

    public DataSet ReturnAsDataSet(string procedureName)
    {
        this.dataSet = new DataSet();   

        OpenConnection();
        NpgsqlTransaction objTransaction = this.Connection.BeginTransaction();

        NpgsqlDataAdapter adapter = new NpgsqlDataAdapter();
        NpgsqlCommand command = this.Connection.CreateCommand();

        try
        {
            NpgsqlParameter refCursorParam1 = new NpgsqlParameter("@ref1", NpgsqlTypes.NpgsqlDbType.Refcursor);
            refCursorParam1.Direction = ParameterDirection.InputOutput;
            refCursorParam1.Value = "ref1";
            command.Parameters.Add(refCursorParam1);  

            refCursorParam2 = new NpgsqlParameter("@ref2", NpgsqlTypes.NpgsqlDbType.Refcursor);
            refCursorParam2.Direction = ParameterDirection.InputOutput;
            refCursorParam2.Value = "ref2";
            command.Parameters.Add(refCursorParam2);

            command.CommandText = "call " + procedureName + "(@ref1, @ref2)";               
            command.Transaction = objTransaction;

            adapter.SelectCommand = command;
            adapter.Fill(dataSet);          

            objTransaction.Commit();                

        }

        catch (NpgsqlException ex)
        {       
            if (objTransaction != null)                
                objTransaction.Rollback();                

            throw new Exception(ex.Message);
        }
        finally
        {
            CloseConnection();
            command.Dispose();
            objTransaction.Dispose();
        }

        return this.dataSet;
    }

此代码将返回一个表,其中以“ ref1”,“ ref2”作为列,并以“ ref1”和“ ref2”作为其中的值,如下所示:

enter image description here

但是我需要从过程中返回的实际结果集。如何在不手动获取那些refcursor数据的情况下实现它。我的意思是不使用“获取所有引用”语句,我们如何通过执行上述ExecuteReader()或adapter。Fill()方法来检索数据。npgsql中是否有任何自动游标取消引用?

如果有人知道,请提供答案。感谢您的提前帮助。

postgresql npgsql
1个回答
0
投票

Npgsql目前尚未为您完成此操作,this issue对其进行了跟踪。您可以看到this long discussions on the pros and cons of this。目前,您必须自己在游标上调用FETCH。

© www.soinside.com 2019 - 2024. All rights reserved.