So in C# whenever I retrieved a tinyint from my MSSQL database I used the following cast.
(int)(byte)reader["MyField"];
However, that cast doesn't seem to work in MySQL.
What I have tried
(byte)reader["MyField"];
and just
(int)reader["MyField"];
Edit 1
Exception
The specified cast is not valid.
Edit 2
This is the data type.
{Name = "SByte" FullName = "System.SByte"}
The problem is that due to casting and explicit operators:
(byte)objectExpressionis is not the same as(byte)sbyteExpression.The first is a [direct] cast which fails because the real object type is
sbyteand notbyte. The latter will perform a conversion that just happens to use a explicit operator (an "Explicit Conversion") with syntax that, unfortunately, still looks like a [direct] cast as per above. Here is an example of it failing sans-database:Either use an
(sbyte)objectExpressioncast which is valid for the real object type, orConvert.ToInt32(objectExpression)which takes anobjectand does some magic to convert it to an int. (UsingConvert.ToBytecould throw an exception on overflow.)Happy coding!