Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/10.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
如何用dot-Net核(C#)读取存储过程游标参数_C#_Postgresql_Asp.net Core_Stored Procedures_Npgsql - Fatal编程技术网

如何用dot-Net核(C#)读取存储过程游标参数

如何用dot-Net核(C#)读取存储过程游标参数,c#,postgresql,asp.net-core,stored-procedures,npgsql,C#,Postgresql,Asp.net Core,Stored Procedures,Npgsql,我试图使用DotnetCore3.1和Npgsql从存储过程中获取数据。我几乎什么都试过了,但没有成功。我能帮上忙吗 PostgresSQL脚本 INSERT INTO DUMMY_TABLE VALUES(1, 'Albert'); INSERT INTO DUMMY_TABLE VALUES(2, 'Albert'); INSERT INTO DUMMY_TABLE VALUES(3, 'James'); CREATE PROCEDURE getId(IN _name text, INOU

我试图使用DotnetCore3.1和Npgsql从存储过程中获取数据。我几乎什么都试过了,但没有成功。我能帮上忙吗

PostgresSQL脚本

INSERT INTO DUMMY_TABLE VALUES(1, 'Albert');
INSERT INTO DUMMY_TABLE VALUES(2, 'Albert');
INSERT INTO DUMMY_TABLE VALUES(3, 'James');

CREATE PROCEDURE getId(IN _name text, INOUT p_refcur refcursor)
    AS
    $BODY$
    BEGIN
        OPEN p_refcur FOR
        select * from DUMMY_TABLE where name = _name;
    END;
    $BODY$
    LANGUAGE plpgsql;
    
call getId('Albert','cursor_name');
FETCH ALL IN "cursor_name";
Dotnet代码Snippit

 var cs = "Host=localhost;Username=postgres;Password=root;Database=temp";
using var con = new NpgsqlConnection(cs);
con.Open();

NpgsqlTransaction tr = (NpgsqlTransaction) con.BeginTransaction();
NpgsqlCommand cursCmd = new NpgsqlCommand("call getId(:param,:ref)", (NpgsqlConnection)con);
cursCmd.Transaction = tr;
NpgsqlParameter rf = new NpgsqlParameter("ref", NpgsqlTypes.NpgsqlDbType.Refcursor);
rf.Value = 'c';
rf.Direction = System.Data.ParameterDirection.InputOutput;

NpgsqlParameter param2 = new NpgsqlParameter("param", NpgsqlTypes.NpgsqlDbType.Text);
param2.Value = "Albert";
rf.Direction = System.Data.ParameterDirection.Input;
cursCmd.Parameters.Add(param2);
cursCmd.Parameters.Add(rf);
// return cursCmd.ExecuteScalar().ToString();

NpgsqlDataReader r = cursCmd.ExecuteReader();
    Console.WriteLine(r.Read());
var version = "default";
while (r.Read())
{
    ; // r.GetValue(0);
    version = r.GetValue(0).ToString();
        Console.WriteLine(r.GetColumnSchema());

}
r.NextResult();
while (r.Read())
{
    ;
        version = r.GetValue(0).ToString();
        Console.WriteLine(r);
}

// tr.Commit();
return version;
我从你那里借用了代码。我同意其他一些实现。谢谢