c# - PostgreSQL:函数返回多个记录集:如何在 C# 中读取?

标签 c# postgresql function recordset

使用这个链接 http://www.sqlines.com/postgresql/npgsql_cs_result_sets 我已经创建了示例,但它不起作用: 这是我的功能:

 CREATE OR REPLACE FUNCTION show_cities_multiple() 
 RETURNS SETOF refcursor AS $$
    DECLARE
      ref1 refcursor; 
      ref2 refcursor;                             
    BEGIN
      OPEN ref1 FOR SELECT user_id, user_name FROM users;
      RETURN NEXT ref1; 

      OPEN ref2 FOR SELECT id, company FROM customers;
      RETURN NEXT ref2;      
    END;
    $$ LANGUAGE plpgsql;

第一个循环仅读取记录集名称,这就是我可以从函数中读取的所有内容。

         NpgsqlConnection conn = new NpgsqlConnection(GetConnectionString());
                conn.Open();
                NpgsqlTransaction tran = conn.BeginTransaction();
                NpgsqlCommand command = new NpgsqlCommand("show_cities_multiple", conn);
                command.CommandType = CommandType.StoredProcedure;
                NpgsqlDataReader dr = command.ExecuteReader();
                while (dr.Read())
                {
                   Console.Write("{0}\n", dr[0]);
                }
// ----------there is output - only names of cursors recordsets
// <unnamed portal 1>
// <unnamed portal 2>

                dr.NextResult(); 
// But there is nothing to read, no additional recordsets
                while (dr.Read())
                    Console.Write("{0}\t{1} \n", dr[0], dr[1]);

                tran.Commit();
                conn.Close();

怎么了?如何从 PGSQL 函数读取多个记录集?

最佳答案

有几种方法可以针对不同的版本使用游标

how can I get cursor data with calling stored procedure in npgsql

关于c# - PostgreSQL:函数返回多个记录集:如何在 C# 中读取?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45783814/

相关文章:

c# - 必须在 new Uri() 中使用 http ://1/? 才能获取 NameValueCollection

c# - 将样式应用于第一个 child ?

c# - 如何将 Microsoft Silverlight for Symbian 编译为 .SIS(独立)应用程序?

postgresql - 在 Docker 容器内运行 `mkdir` 时权限被拒绝

检查函数参数的类型,C

ListAppend() 不起作用?

Jquery检查元素是否隐藏(连续)

c# - 在 ASP.NET 4 中更改图表控件的图表类型

postgresql - 如何使用 CLI for Cloud Composer 添加 SSL postgres 连接?

xml - SQLAlchemy TypeDecorator 不起作用