I have the following stored procedure in MySQL that contains an output parameter. The stored procedure works when executed on the DB server.
CALL `db`.`usp_AgentTransferPortalsDisplayed`(@val);
select @val
I am trying to call this stored procedure using Entity Framework Core using the following.
var count = ctx.Database.ExecuteSqlCommand("CALL db.usp_AgentTransferPortalsDisplayed(@output);",
new MySqlParameter("@output", MySqlDbType.Int32));
I receive the following error:
MySql.Data.MySqlClient.MySqlException has been thrown: "OUT or INOUT argument 1 for routine db.usp_AgentTransferPortalsDisplayed is not a variable or NEW pseudo-variable in BEFORE trigger"
I am not sure if I am using the proper syntax since this is an output parameter or not.
I also tried this way:
var outParam = new MySqlParameter();
outParam.Direction = ParameterDirection.Output;
outParam.DbType = DbType.Int32;
var count = ctx.Database.ExecuteSqlCommand("CALL db.usp_AgentTransferPortalsDisplayed(@outParam);", outParam);
MySql.Data.MySqlClient.MySqlException: "Fatal error encountered during command execution." ---> System.Exception {MySql.Data.MySqlClient.MySqlException}: "Parameter '@outParam' must be defined." at at
UPDATE
Based on the links that Matt suggested; I was able to do it using this syntax:
using (MySqlConnection lconn = new MySqlConnection(conn))
{
lconn.Open();
using (MySqlCommand cmd = new MySqlCommand())
{
cmd.Connection = lconn;
cmd.CommandText = "db.usp_AgentTransferPortalsDisplayed"; // The name of the Stored Proc
cmd.CommandType = CommandType.StoredProcedure; // It is a Stored Proc
cmd.Parameters.AddWithValue("@result", MySqlDbType.Int32);
cmd.Parameters["@result"].Direction = ParameterDirection.Output; // from System.Data
cmd.ExecuteNonQuery(); // let it rip
Object obj = cmd.Parameters["@result"].Value;
var lParam = (Int32)obj; // more useful datatype
}
}