2017-08-20 8 views
1

私は例を作成しているが、それは動作しません。このリンク http://www.sqlines.com/postgresql/npgsql_cs_result_sets を使用する: 私の機能があります:PostgreSQL:関数は複数のレコードセットを返します:C#での読み込み方法?

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関数から複数のレコードセットを読み込むには?

+0

で動作するようにいくつかの方法は、あなたが、その後 '最初の結果セットを取得すると、'すべてのフェッチ「<無名ポータル1>」から全てをフェッチ '実行する必要がありますありますそれに応じて2番目の結果セットを取得します(関数から返された値で名前を置き換えます)。 [ドキュメンテーションとサンプル](https://www.postgresql.org/docs/current/static/plpgsql-cursors.html#AEN66596) – Abelisto

+0

別のNpgsqlCommandにする必要がありますか?私の例ではどこでこれらのコマンドを実行する必要がありますか? – Oleg

+0

はい、別のコマンドにする必要があります。実行する場所はあなた次第ですが、関数と同じトランザクションで実行する必要があります。 – Abelisto

答えて

関連する問題