Search code examples
c#stored-proceduresdappernpgsqlref-cursor

Dapper/Npgsql stored procedure with refcursor parameter query


I am using Dapper, I know how to access normal stored procedures as mentioned here, however, how do I pass on the the Npgsql refcursor name to the proc (using C#)? For example:

I have a proc that looks like:

FUNCTION xx.getData(
    v_ref refcursor,
    v_id integer)
  RETURNS refcursor AS
...

How would I specify the parameters for xx.getData?

For example, if getData accepted just one parameter of type int, then I could call it like so:

var data = cnn.Query<myType>("xx.getData", new {Id = 1}, 
        commandType: CommandType.StoredProcedure);

OR

var p = new DynamicParameters();
p.Add("@id", 11);
cnn.Execute("xx.getData", p, commandType: CommandType.StoredProcedure); 

I can't find the correct type in System.DbType to pass on in the query.


Solution

  • Note that a refcursor corresponds to an active cursor that has already been opened in a previous call. In other words, it does not correspond to a stored procedure, but rather to a resultset (possibly but not necessarily returned from a stored procedure).

    Just in case you really do need to send a refcursor, what you're looking for is NpgsqlDbType.Refcursor.