Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# PostgreSQL:函数返回多个记录集:如何在c中读取?_C#_Postgresql_Function_Recordset - Fatal编程技术网

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

C# PostgreSQL:函数返回多个记录集:如何在c中读取?,c#,postgresql,function,recordset,C#,Postgresql,Function,Recordset,使用此链接 我创建了该示例,但它不起作用: 这是我的职责: CREATE OR REPLACE FUNCTION show_cities_multiple() RETURNS SETOF refcursor AS $$ DECLARE ref1 refcursor; ref2 refcursor; BEGIN OPEN ref1 FOR SELECT user_id, user_

使用此链接 我创建了该示例,但它不起作用: 这是我的职责:

 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函数中读取多个记录集?

有几种方法可用于不同版本的游标


然后,您应该执行fetchall-from以获取第一个结果集,并相应地使用函数返回的值替换名称以获取第二个结果集。它应该是单独的npgsql命令吗?或者在我的示例中,我需要在哪里执行这些命令?是的,应该是单独的命令。执行它们的位置由您决定,但应该在与函数相同的事务中执行。这是否意味着代码不正确?好的,只是用mono测试了一下,代码和你的差不多。我从康涅狄格州的show_cities_multiple中使用NpgsqlCommand=new NpgsqlCommandselect*得到了您描述的结果;代替NpgsqlCommand=new NpgsqlCommandshow\u cities\u multiple,conn;command.CommandType=CommandType.storedProcess;仅供参考。