I'm using PostgreSQL with a .NET project, and I'm facing an issue executing a stored procedure through Dapper when using dynamic parameters to pass a DATE parameter.
The stored procedure is defined as follows:
CREATE OR REPLACE PROCEDURE simplemenuproc(
appcode IN VARCHAR,
myDtTm DATE,
ref1 INOUT REFCURSOR
)
LANGUAGE plpgsql
AS $BODY$
-- Procedure body
$BODY$;
Here's the C# code I'm using to call it:
NpgsqlTransaction tran = connection.BeginTransaction();
var parameters = new DynamicParameters();
parameters.Add("appcode", "PPM", direction: ParameterDirection.Input);
parameters.Add("myDtTm", DateOnly.Parse(DateTime.UtcNow.ToString("yyyy-MM-dd")), DbType.Date, direction: ParameterDirection.Input);
parameters.Add("ref1", direction: ParameterDirection.InputOutput);
// Execute the stored procedure
await connection.ExecuteAsync(storedProcedureName, parameters, commandType: CommandType.StoredProcedure);
When I try to execute this, I get the following error:
42883: procedure simplemenuproc(appcode => text, myDtTm => date, ref1 => unknown) does not exist
What I've tried
Changing how the date parameter is passed:
parameters.Add("myDtTm", DateTime.UtcNow.Date, direction: ParameterDirection.Input);
This resulted in:
42883: procedure simplemenuproc(appcode => text, myDtTm => timestamp with time zone, ref1 => unknown) does not exist
Verifying the stored procedure name and parameter types. The myDtTm parameter is defined as DATE, but it seems like the data type is mismatched when passed.
My environment:
- Database: PostgreSQL
- ORM: Dapper
- Driver: Npgsql
Question
How do I correctly pass a
DATEparameter to a PostgreSQL stored procedure using dynamic parameters in Dapper and Npgsql?Why is the procedure not found even when the parameter types seem to match?
? Dates have no format, they're binary values, both in the database and .NET. UseDateOnly.Fro mDateTime(DateTime.Today)` orDateOnly.FromDateTime(DateTime.UtCNow)to create aDateOnlyrepresenting the current date.simplemenuprocprocedure while the question containsproc_Name. Are you surestoredProcedureNameis correct?parameters.Add("ref1", NpgsqlDbType.Refcursor).Direction = ParameterDirection.InputOutput;