如何在npgsql中调用存储过程获取游标数据 [英] how can I get cursor data with calling stored procedure in npgsql

查看:368
本文介绍了如何在npgsql中调用存储过程获取游标数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经研究了 www.npgsql.org 中的资料,但找不到解决问题的方法...



表,PostgreSQL

  [城市],[州] 
奥斯丁, TX
休斯顿, TX
洛杉矶, CA
圣地亚哥 , CA
圣弗朗西斯科; CA
圣路易斯, MO

函数(存储过程),PostgreSQL

 -过程返回单个结果集(游标)
创建或替换功能show_cities()返回refsors as $$
DECLARE
ref refcursor;
开始
为城市选择州/省的公开参考;
RETURN ref;
END;
$$语言plpgsql;

代码,C#

 使用(NpgsqlConnection conn = new NpgsqlConnection(ConfigurationManager.ConnectionStrings [ dbConnection]。ConnectionString)))
{
conn.Open();
使用(NpgsqlTransaction tran = conn.BeginTransaction())
{
使用(var命令=新的NpgsqlCommand( show_cities,conn))
{
命令。交易= tran;
command.CommandType = CommandType.StoredProcedure;
NpgsqlDataReader dr = command.ExecuteReader();

而(dr.Read())
str + = dr.GetValue(0);

dr.Close();
}
tran.Commit();
}
}

这将返回未命名的门户1,它是一个游标要获取而不是获取数据,
是否可以将其转换为诸如 Austin,Houston,Los Angeles ... 的数据?



在互联网上有一些关于此的帖子,但是我不确定自己在做什么错。




  • npgsql:ver3。 0.3

  • c#:vs2012



(添加)
我发现这是发生在npgsql ver3.x,而在ver2.x中,它与我的代码配合正常。



(参考)
http://www.sqlines.com/postgresql/npgsql_cs_result_sets

解决方案

Npgsql 2.x具有一项功能,可以自动取消引用从函数返回的游标。该功能从Npgsql 3.0删除;我们的3.0迁移节点中提到了这一点,讨论内容在此问题中。由于游标只是返回而没有取消引用,因此Npgsql返回游标名称本身(未命名的门户1);您现在可以通过发送 FETCH 等从此查询中获取结果。



但是,如上所述,只包装一个函数中的SELECT没有多大意义。如果确实需要编写一个返回 single 结果集的函数,请使其返回SETOF或TABLE而不是游标: CREATE FUNCTION ... RETURNS TABLE(column_name column_type [,...])。除了更简单,更简洁之外,这还更加高效,因为直接返回查询结果(取消引用游标涉及另一个数据库往返)。



请参见 PostgreSQL文档有关如何定义返回表的函数的更多信息。 / p>

I have looked into materials in www.npgsql.org, but couldn't find how to solve my problem...

Table, PostgreSQL

[City], [State]
"Austin", "TX"
"Houston", "TX"
"Los Angeles", "CA"
"San Diego", "CA"
"San Fransisco";"CA"
"St.Louis", "MO"

Function (stored procedure), PostgreSQL

-- Procedure that returns a single result set (cursor) 
   CREATE OR REPLACE FUNCTION show_cities() RETURNS refcursor AS $$
    DECLARE
      ref refcursor;
    BEGIN
      OPEN ref FOR SELECT city, state FROM cities;
      RETURN ref;                                 
    END;
    $$ LANGUAGE plpgsql;

Code, C#

using (NpgsqlConnection conn = new NpgsqlConnection(ConfigurationManager.ConnectionStrings["dbConnection"].ConnectionString))
{
    conn.Open();
    using (NpgsqlTransaction tran = conn.BeginTransaction())
    {
        using (var command = new NpgsqlCommand("show_cities", conn))
        {
            command.Transaction = tran;
            command.CommandType = CommandType.StoredProcedure;
            NpgsqlDataReader dr = command.ExecuteReader();

            while (dr.Read())
                str += dr.GetValue(0);

            dr.Close();
        }
        tran.Commit();
    }
}

This returns "unnamed portal 1" and it's a cursor to be fetched not data, Is there any way to convert this to data like Austin, Houston, Los Angeles... ?

There are some posts over internet about this, but I'm not sure what I'm doing wrong.

  • npgsql : ver3.0.3
  • c# : vs2012

(added) I have found this is happening at npgsql ver3.x, while in ver2.x it is working fine with my code. Is there any change in usage for fetching cursor ?

(reference) http://www.sqlines.com/postgresql/npgsql_cs_result_sets

解决方案

Npgsql 2.x had a feature whereby it automatically "dereferenced" cursors returned from functions. This feature was dropped from Npgsql 3.0; this is mentioned in our migration nodes for 3.0, and the discussion is in this issue. Since the cursor is simply returned and isn't dereferenced, Npgsql returns the cursor name itself (unnamed portal 1); you can now fetch results from this query by sending FETCH etc.

However, as was mentioned, wrapping a single SELECT in a function doesn't make much sense. If you do need to write a function that returns a single resultset, make it return a SETOF or a TABLE instead of a cursor: CREATE FUNCTION ... RETURNS TABLE (column_name column_type [, ...]). Apart from being simpler and cleaner, this is also more efficient, as the query results are returned directly (dereferencing the cursor involves another database roundtrip).

See the PostgreSQL docs for more info on how to define a function returning a table.

这篇关于如何在npgsql中调用存储过程获取游标数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆