Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. C# MySQL Question

C# MySQL Question

Scheduled Pinned Locked Moved C#
helpquestioncsharpmysql
21 Posts 13 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D d87c

    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

    L Offline
    L Offline
    Lost User
    wrote on last edited by
    #9

    MySQL TinyInt maps to C#'s byte. Use byte.Parse().

    L 1 Reply Last reply
    0
    • L Lost User

      MySQL TinyInt maps to C#'s byte. Use byte.Parse().

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #10

      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

      L 1 Reply Last reply
      0
      • D d87c

        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

        S Offline
        S Offline
        Simon Bang Terkildsen
        wrote on last edited by
        #11

        you should use Convert.ToInt32(myByte) to convert byte to int Convert class on MSDN[^]

        1 Reply Last reply
        0
        • D d87c

          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

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #12

          reader["someField"] returns an object of some type, probably byte or sbyte. You can see which by looking at reader["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

          M 1 Reply Last reply
          0
          • P PIEBALDconsult

            How about an sbyte then?

            M Offline
            M Offline
            marssilen
            wrote on last edited by
            #13

            [removed]

            realJSOPR 1 Reply Last reply
            0
            • M marssilen

              [removed]

              realJSOPR Offline
              realJSOPR Offline
              realJSOP
              wrote on last edited by
              #14

              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

              OriginalGriffO G 2 Replies Last reply
              0
              • realJSOPR realJSOP

                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

                OriginalGriffO Offline
                OriginalGriffO Offline
                OriginalGriff
                wrote on last edited by
                #15

                Normally, 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."

                "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                1 Reply Last reply
                0
                • D Dave Kreskowiak

                  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

                  M Offline
                  M Offline
                  marssilen
                  wrote on last edited by
                  #16
                  1 Reply Last reply
                  0
                  • L Luc Pattyn

                    reader["someField"] returns an object of some type, probably byte or sbyte. You can see which by looking at reader["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

                    M Offline
                    M Offline
                    marssilen
                    wrote on last edited by
                    #17
                    1 Reply Last reply
                    0
                    • realJSOPR realJSOP

                      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

                      G Offline
                      G Offline
                      GenJerDan
                      wrote on last edited by
                      #18

                      Ah, the dreaded inerhtml attack. ;P

                      Ain't nothin' in the circus for free, kid. My Mu[sic] My Films My Windows Programs, etc.

                      1 Reply Last reply
                      0
                      • L Luc Pattyn

                        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

                        L Offline
                        L Offline
                        Lost User
                        wrote on last edited by
                        #19

                        My apologies. I wanted to ask the OP to use Convert.ToByte() or an explicit cast like (byte)reader["col1"], but I was carried away by the OP's use of Parse/TryParse. Thanks for pointing out. :-)

                        1 Reply Last reply
                        0
                        • D d87c

                          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

                          G Offline
                          G Offline
                          Gonzalo Cao
                          wrote on last edited by
                          #20

                          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 :)

                          1 Reply Last reply
                          0
                          • Richard Andrew x64R Richard Andrew x64

                            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 try short. Try this:

                            int newNumber = (int)(short)reader["someField"];

                            The difficult we do right away... ...the impossible takes slightly longer.

                            B Offline
                            B Offline
                            BoxyBrown
                            wrote on last edited by
                            #21

                            I think casting (int)(short) is redundant. Here is good table for mapping clr to sql types http://msdn.microsoft.com/en-us/library/bb386947.aspx[^]

                            1 Reply Last reply
                            0
                            Reply
                            • Reply as topic
                            Log in to reply
                            • Oldest to Newest
                            • Newest to Oldest
                            • Most Votes


                            • Login

                            • Don't have an account? Register

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • World
                            • Users
                            • Groups