简体   繁体   English

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

[英]PostgreSQL: function returns multiple recordsets: how to read in c#?

Using this link http://www.sqlines.com/postgresql/npgsql_cs_result_sets I have created the example, but it does not work: There is my function: 使用此链接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;

The 1st loop reads recordsets names only and that is all what I can read from the function. 第一个循环仅读取记录集名称,这就是我可以从函数中读取的全部内容。

         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();

What is wrong? 怎么了? How to read multiple recordsets from a PGSQL function? 如何从PGSQL函数读取多个记录集?

There are a few ways to work with cursors for different versions 有几种方法可以使用不同版本的游标

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM