How do I retrieve a data type of tinyint from MySQL in C#?
Asked Answered
A

3

10

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"}
Arri answered 6/6, 2012 at 22:0 Comment(6)
Have you tried reader["MyField"] + 0;?Babiche
@Hans That won't compile: the expression is typed object + int there, for which there is no matching + operator.Starlet
Have you tried int.Parse(reader["MyField"].ToString());?Babiche
@Hans That would actually work ... but why? ;-)Starlet
The two methods I've described are the laziest ways to program between different data types and actually get values back. The first one will automatically invoke any implicit cast that object to byte,int,double,float,or long, which can be then cast to int. The second one physically takes the ToString() of the object, which, if correctly implemented for the type of your object, should return a string of digits which int.Parse should be able to read as an int. These methods require no knowledge of what the datatypes are and instead use (perhaps inefficient) type specific methods.Babiche
And yes, the last one works for Nullable<T> as well.Babiche
S
10

The problem is that due to casting and explicit operators:

(byte)objectExpression is is not the same as (byte)sbyteExpression.

The first is a [direct] cast which fails because the real object type is sbyte and not byte. 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:

var obj = (object)(sbyte)0;
var i1 = (int)(sbyte)obj;  // okay: object (cast)-> sbyte (conversion)-> int
var i2 = (int)obj;         // fail: sbyte (cast)-> int (but sbyte is not int!)

Either use an (sbyte)objectExpression cast which is valid for the real object type, or Convert.ToInt32(objectExpression) which takes an object and does some magic to convert it to an int. (Using Convert.ToByte could throw an exception on overflow.)

Happy coding!

Starlet answered 6/6, 2012 at 22:7 Comment(2)
Correct! gotta wait 6 minutes.Arri
+1, seriously this had bitten me always and left clueless, till i read this answer!Niobe
S
10

To determine the proper type, look at the value of

reader["MyField"].GetType()

in the debugger.

Scalenus answered 6/6, 2012 at 22:1 Comment(6)
Your a genius! Let me look real quickArri
(A great comment... but it doesn't answer the "how to" as and of itself...)Starlet
I try to help people help themselves.Scalenus
@webdad3 it provides a clear method to find out how the code should look like. The OP must do a tiny step in addition to this to solve the problem.Scalenus
VS2017: "This expression causes side effects and will not be evaluated "Exuberant
@Exuberant that message can happen when the debugger refuses to evaluate an expression. The actual program is fine.Scalenus
S
10

The problem is that due to casting and explicit operators:

(byte)objectExpression is is not the same as (byte)sbyteExpression.

The first is a [direct] cast which fails because the real object type is sbyte and not byte. 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:

var obj = (object)(sbyte)0;
var i1 = (int)(sbyte)obj;  // okay: object (cast)-> sbyte (conversion)-> int
var i2 = (int)obj;         // fail: sbyte (cast)-> int (but sbyte is not int!)

Either use an (sbyte)objectExpression cast which is valid for the real object type, or Convert.ToInt32(objectExpression) which takes an object and does some magic to convert it to an int. (Using Convert.ToByte could throw an exception on overflow.)

Happy coding!

Starlet answered 6/6, 2012 at 22:7 Comment(2)
Correct! gotta wait 6 minutes.Arri
+1, seriously this had bitten me always and left clueless, till i read this answer!Niobe
A
0

May I suggest to let the system work against itself? The DataReader class provides features for getting the correct type of value:

reader.GetInt32("id");
reader.GetByte("MyByteField");

This way the Reader provides you with the type you expect.

Autry answered 23/2, 2021 at 14:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.