C# MySQL Question
-
The reader returns type
object
, and it must be cast to the necessary type for use in function calls. For the TinyInt type, I forget exactly what you need to cast it to but you can tryshort
. Try this:int newNumber = (int)(short)reader["someField"];
The difficult we do right away... ...the impossible takes slightly longer.
-
What about:
int newNumber = (int)(byte)reader["fieldName"];
The difficult we do right away... ...the impossible takes slightly longer.
-
Hi All, I have a problem I have a MySQL Table, one of the columns' Data Type is TinyInt(1) When I use this line of code it gives an error int newNumber = int.Parse(string.Format("{0}", reader["someField"])); I think the reader is reading a bool value however MySQL Table has row values of 0 & 1 & 6 & 7 so it is not bool true/false type How can I actually grab that value to store into "int newNumber" Thanks
A MySQL TinyInt is 1 byte. That type in C# i, obviously, byte. BTW, why on earth are you converting the returned database value to a string, only to parse it back to an int??
A guide to posting questions on CodeProject[^]
Dave Kreskowiak -
What about:
int newNumber = (int)(byte)reader["fieldName"];
The difficult we do right away... ...the impossible takes slightly longer.
-
A MySQL TinyInt is 1 byte. That type in C# i, obviously, byte. BTW, why on earth are you converting the returned database value to a string, only to parse it back to an int??
A guide to posting questions on CodeProject[^]
Dave Kreskowiakmy friend's db is mysql and I am trying to grab his data and then store the value into my sql db Im grabbing the mysql data parse into a collect list its a WCF project so I created a datamember of int newNumber ... thats why I need to grab it int newNumber = (int)(byte)reader["fieldName"]; doesn't work >< it looks like it is reading as a bool value cause if i convert it to a string, it will gives false since in that column, I have values of 0, 4, 6 ,7
-
How about an
sbyte
then? -
Hi All, I have a problem I have a MySQL Table, one of the columns' Data Type is TinyInt(1) When I use this line of code it gives an error int newNumber = int.Parse(string.Format("{0}", reader["someField"])); I think the reader is reading a bool value however MySQL Table has row values of 0 & 1 & 6 & 7 so it is not bool true/false type How can I actually grab that value to store into "int newNumber" Thanks
-
using strings and sometype.Parse/TryParse is a complete waste, as Dave already pointed out. all that is required here is a simple numeric-to-numeric cast. :(
Luc Pattyn [My Articles] Nil Volentibus Arduum
-
my friend's db is mysql and I am trying to grab his data and then store the value into my sql db Im grabbing the mysql data parse into a collect list its a WCF project so I created a datamember of int newNumber ... thats why I need to grab it int newNumber = (int)(byte)reader["fieldName"]; doesn't work >< it looks like it is reading as a bool value cause if i convert it to a string, it will gives false since in that column, I have values of 0, 4, 6 ,7
you should use
Convert.ToInt32(myByte)
to convert byte to int Convert class on MSDN[^] -
Hi All, I have a problem I have a MySQL Table, one of the columns' Data Type is TinyInt(1) When I use this line of code it gives an error int newNumber = int.Parse(string.Format("{0}", reader["someField"])); I think the reader is reading a bool value however MySQL Table has row values of 0 & 1 & 6 & 7 so it is not bool true/false type How can I actually grab that value to store into "int newNumber" Thanks
reader["someField"]
returns an object of some type, probablybyte
orsbyte
. You can see which by looking atreader["someField"].GetType().ToString()
Once you know the .NET type of the incoming data just do two casts:(typeYouWant)(typeItIsNow)reader["someField"]
BTWL The alternative of course is to change the field type in MySQL itself, i.e. modifying the database. :)
Luc Pattyn [My Articles] Nil Volentibus Arduum
-
How about an
sbyte
then? -
Nice try, retard. What makes you think a web site by and for programmers is not going to have that base covered? Oh, and by the way, your account will be deleted in the next 30 minutes or so.
".45 ACP - because shooting twice is just silly" - JSOP, 2010
-----
You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
-----
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass." - Dale Earnhardt, 1997 -
Nice try, retard. What makes you think a web site by and for programmers is not going to have that base covered? Oh, and by the way, your account will be deleted in the next 30 minutes or so.
".45 ACP - because shooting twice is just silly" - JSOP, 2010
-----
You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
-----
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass." - Dale Earnhardt, 1997Normally, I do not agree with your occasional use of "retard". In this case however, it is accurate and justified. 5! :laugh:
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together. Manfred R. Bihy: "Looks as if OP is learning resistant."
-
A MySQL TinyInt is 1 byte. That type in C# i, obviously, byte. BTW, why on earth are you converting the returned database value to a string, only to parse it back to an int??
A guide to posting questions on CodeProject[^]
Dave Kreskowiak -
reader["someField"]
returns an object of some type, probablybyte
orsbyte
. You can see which by looking atreader["someField"].GetType().ToString()
Once you know the .NET type of the incoming data just do two casts:(typeYouWant)(typeItIsNow)reader["someField"]
BTWL The alternative of course is to change the field type in MySQL itself, i.e. modifying the database. :)
Luc Pattyn [My Articles] Nil Volentibus Arduum
-
Nice try, retard. What makes you think a web site by and for programmers is not going to have that base covered? Oh, and by the way, your account will be deleted in the next 30 minutes or so.
".45 ACP - because shooting twice is just silly" - JSOP, 2010
-----
You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
-----
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass." - Dale Earnhardt, 1997 -
using strings and sometype.Parse/TryParse is a complete waste, as Dave already pointed out. all that is required here is a simple numeric-to-numeric cast. :(
Luc Pattyn [My Articles] Nil Volentibus Arduum
-
Hi All, I have a problem I have a MySQL Table, one of the columns' Data Type is TinyInt(1) When I use this line of code it gives an error int newNumber = int.Parse(string.Format("{0}", reader["someField"])); I think the reader is reading a bool value however MySQL Table has row values of 0 & 1 & 6 & 7 so it is not bool true/false type How can I actually grab that value to store into "int newNumber" Thanks
Actually this is not your fault, MySQL reader transforms TINYINT to BOOL by default. In order to prevent this, you need to add the following line to you connection string: "Treat Tiny As Boolean = false". Just by doing this, TINYINT will be treated as a numeric value :)
-
The reader returns type
object
, and it must be cast to the necessary type for use in function calls. For the TinyInt type, I forget exactly what you need to cast it to but you can tryshort
. Try this:int newNumber = (int)(short)reader["someField"];
The difficult we do right away... ...the impossible takes slightly longer.