Search code examples
postgresqlnpgsqlpetapoco

How to execute a PostgreSQL stored procedure from PetaPoco (with Npgsql)?


Given a PostgreSQL (9.5) stored procedure (with Npgsql driver) as:

CREATE OR REPLACE FUNCTION "GetAllDx"( patient_recid integer, tencounter timestamp without time zone) RETURNS SETOF view_dx

How is this executed from PetaPoco ? Can it be done? (I've been using Dapper).

Any help is much appreciated.


Solution

  • Very simple,

    [TestMethod]
            public void GetAllPatientsWithServices()
            {
                // Create a PetaPoco database object
                var db = new chaosDB("localconnection");
    
                // Calling stored procedure getallpatientswithservices()
                var a =  db.Fetch<view_patient>("Select * from getallpatientswithservices()");
    
                foreach( var b in a)
                {
                    Console.WriteLine("{0}    -  {1}", b.cpatient, b.chart_number);
                }
            }
    

    Or, with a mixed-case procedure name:

    [TestMethod]
            public void GetDxLibrary()
            {
                // Create a PetaPoco database object
                var db = new chaosDB("localconnection");
    
                // Calling stored procedure with mixed case name
                var a = db.Fetch<icd9>("Select * from \"GetDxLibrary\"()");
    
                foreach (var b in a)
                {
                    Console.WriteLine("{0}    -  {1}", b.code,b.cdesc);
                }
            }