Description:
Setting MySqlParameter.MySqlDbType to MySqlDbType.Int24 throws an error similar to "Out of range value for column 'data' at row 1" when a prepared command is executed and the parameter is a negative number
When "command.ExecuteNonQuery()" is executed in the attached sample, the following exception is thrown:
MySql.Data.MySqlClient.MySqlException (0x80004005): Out of range value for column 'data' at row 1
at MySql.Data.MySqlClient.MySqlStream.ReadPacket()
at MySql.Data.MySqlClient.NativeDriver.GetResult(Int32& affectedRow, Int64& insertedId)
at MySql.Data.MySqlClient.Driver.GetResult(Int32 statementId, Int32& affectedRows, Int64& insertedId)
at MySql.Data.MySqlClient.Driver.NextResult(Int32 statementId, Boolean force)
at MySql.Data.MySqlClient.MySqlDataReader.NextResult()
at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior)
at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader()
at MySql.Data.MySqlClient.MySqlCommand.ExecuteNonQuery()
How to repeat:
Execute the following C#:
using var connection = new MySqlConnection("...");
connection.Open();
using (var command = new MySqlCommand(@"drop table if exists prepared_statement_test;
create table prepared_statement_test(data MEDIUMINT);", connection))
{
command.ExecuteNonQuery();
}
using (var command = new MySqlCommand(@"insert into prepared_statement_test(data) values(@data);", connection))
{
command.Parameters.AddWithValue("@data", -1).MySqlDbType = MySqlDbType.Int24;
command.Prepare();
command.ExecuteNonQuery();
}
It can be worked around by writing:
command.Parameters.AddWithValue("@data", -1).MySqlDbType = MySqlDbType.Int32;
or:
command.Parameters.AddWithValue("@data", -1);
Suggested fix:
This appears to be an incomplete fix for bug #95986.
Most likely, the UNSIGNED_MASK should not be getting added here: https://github.com/mysql/mysql-connector-net/blob/56ea6cf19d7b65e874efc2b26b579560e8126b76...