0

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
        }
    }
Flea
  • 11,176
  • 6
  • 72
  • 83
  • check this [answer](https://stackoverflow.com/a/14586951/9534819) – Matt.G Mar 04 '19 at 20:49
  • @Matt.G thanks for the link Matt; I just tried that way and received a different error. I updated my original post to include that. – Flea Mar 04 '19 at 21:13
  • take a look at [this](https://stackoverflow.com/q/51280676/9534819) – Matt.G Mar 04 '19 at 21:19
  • 1
    @Matt.G thanks Matt; although I wasn't able to use the EF syntax; using the more traditional approach did work. Thank you for the links. – Flea Mar 04 '19 at 21:41
  • There is a known issue for [MySqlParameter with Direction=Output does not work for Text commands](https://bugs.mysql.com/bug.php?id=75267). – Mark G Dec 03 '19 at 22:23

0 Answers0