簡體   English   中英

PostgreSQL:函數返回多個記錄集:如何在C#中讀取?

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

使用此鏈接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函數讀取多個記錄集?

有幾種方法可以使用不同版本的游標

如何在npgsql中調用存儲過程獲取游標數據

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM