error with Insert and mysql

Asked

Viewed 25 times

0

I have the following method:

 public byte[] CarregarArquivoImagem( Int64 idVenda,string nomeArquivo, string caminhoArquivo, int ImagemTamanhoMaximo)
    {
        string query = null;
        MySqlDataReader reader;
        MySqlCommand cmd = new MySqlCommand();

        byte[] imagemBytes = null;

        using (MySqlConnection con = new MySqlConnection(ClassMaster.mysqlConection().ConnectionString))
        {
            try
            {                    
                MySqlParameter imagem01 = new MySqlParameter();
                imagem01.ParameterName = "@imagem01";
                imagem01.Value = "imagem01";
                imagem01.DbType = System.Data.DbType.String;

                MySqlParameter imagem02 = new MySqlParameter();
                imagem02.ParameterName = "@imagem02";
                imagem02.Value = "imagem02";
                imagem02.DbType = System.Data.DbType.String;

                query = "INSERT INTO venda_imagem (id, venda_id_venda, imagem_cupom, imagem_receita) VALUES('21121242132', 69," +
                    "@imagem01,@imagem02)";

                con.Open();

                cmd = new MySqlCommand(query, con);
                reader = cmd.ExecuteReader();
                con.Close();

                return imagemBytes;
            }
            catch (MySqlException exx)
            {
                con.Close();
            }
            catch (Exception ex)
            {
                con.Close();
            }
        }

        return imagemBytes;
    }

is returning the following error:

Fatal error encountered During command Execution.

in Innerexception returns the following:

Parameter '@imagem01' must be defined.

what is wrong? , the parameter has been set, can’t understand where the error is.

1 answer

0

According to an example in https://zetcode.com/csharp/mysql/ you need to add the parameters to the command. Taken from the example:

        var sql = "INSERT INTO cars(name, price) VALUES(@name, @price)";
        using var cmd = new MySqlCommand(sql, con);

        cmd.Parameters.AddWithValue("@name", "BMW");
        cmd.Parameters.AddWithValue("@price", 36600);
        cmd.Prepare();

        cmd.ExecuteNonQuery();
  • did as suggested, gave the same error, am using . net core, will q might be a bug?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.