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
  1. Home
  2. Other Discussions
  3. The Weird and The Wonderful
  4. Data Types And Convenience Methods are Overrated

Data Types And Convenience Methods are Overrated

Scheduled Pinned Locked Moved The Weird and The Wonderful
com
17 Posts 9 Posters 0 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.
  • A Offline
    A Offline
    AspDotNetDev
    wrote on last edited by
    #1

    cmd.Parameters.Add("@SomeID", SqlDbType.Int)
    cmd.Parameters("@SomeID").Value = Session("SomeID").ToString()

    It appears this developer didn't have a strong conception of data types, knowledge of AddWithValue, or knowledge that the "@" is optional when specifying parameters. :doh:

    Thou mewling ill-breeding pignut!

    P J R J L 6 Replies Last reply
    0
    • A AspDotNetDev

      cmd.Parameters.Add("@SomeID", SqlDbType.Int)
      cmd.Parameters("@SomeID").Value = Session("SomeID").ToString()

      It appears this developer didn't have a strong conception of data types, knowledge of AddWithValue, or knowledge that the "@" is optional when specifying parameters. :doh:

      Thou mewling ill-breeding pignut!

      P Offline
      P Offline
      PIEBALDconsult
      wrote on last edited by
      #2

      AspDotNetDev wrote:

      SqlDbType.Int

      Soooo many practitioners think that's necessary. :sigh:

      AspDotNetDev wrote:

      AddWithValue

      I never use that; it's not defined by an Interface I use. I use IDbCommand.CreateParameter and IDataParameterCollection.Add.

      AspDotNetDev wrote:

      is optional

      That may be true with the database system you use; it may not be true with all the database systems I use.

      OriginalGriffO A 2 Replies Last reply
      0
      • P PIEBALDconsult

        AspDotNetDev wrote:

        SqlDbType.Int

        Soooo many practitioners think that's necessary. :sigh:

        AspDotNetDev wrote:

        AddWithValue

        I never use that; it's not defined by an Interface I use. I use IDbCommand.CreateParameter and IDataParameterCollection.Add.

        AspDotNetDev wrote:

        is optional

        That may be true with the database system you use; it may not be true with all the database systems I use.

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

        I also prefer to see it, even if optional because it makes it more obvious that it is a required parameter.

        If you get an email telling you that you can catch Swine Flu from tinned pork then just delete it. It's Spam.

        "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
        • A AspDotNetDev

          cmd.Parameters.Add("@SomeID", SqlDbType.Int)
          cmd.Parameters("@SomeID").Value = Session("SomeID").ToString()

          It appears this developer didn't have a strong conception of data types, knowledge of AddWithValue, or knowledge that the "@" is optional when specifying parameters. :doh:

          Thou mewling ill-breeding pignut!

          J Offline
          J Offline
          Johnny J
          wrote on last edited by
          #4

          AddWithValue is the best thing since sliced bread (except for bacon)... Makes everything a hell of a lot easier. :thumbsup:

          Why can't I be applicable like John? - Me, April 2011
          -----
          Beidh ceol, caint agus craic againn - Seán Bán Breathnach
          -----
          Da mihi sis crustum Etruscum cum omnibus in eo!
          -----
          Just because a thing is new don’t mean that it’s better - Will Rogers, September 4, 1932

          O P 2 Replies Last reply
          0
          • A AspDotNetDev

            cmd.Parameters.Add("@SomeID", SqlDbType.Int)
            cmd.Parameters("@SomeID").Value = Session("SomeID").ToString()

            It appears this developer didn't have a strong conception of data types, knowledge of AddWithValue, or knowledge that the "@" is optional when specifying parameters. :doh:

            Thou mewling ill-breeding pignut!

            R Offline
            R Offline
            R Giskard Reventlov
            wrote on last edited by
            #5

            AddWithValue can cause problems where conversions may not be as expected: better to use Add and be explicit about what you are doing. And what harm the @? Just because something is optional doesn't mean you shouldn't use it - verbosity is king - it promotes clarity.

            "If you think it's expensive to hire a professional to do the job, wait until you hire an amateur." Red Adair. nils illegitimus carborundum me, me, me

            A 1 Reply Last reply
            0
            • R R Giskard Reventlov

              AddWithValue can cause problems where conversions may not be as expected: better to use Add and be explicit about what you are doing. And what harm the @? Just because something is optional doesn't mean you shouldn't use it - verbosity is king - it promotes clarity.

              "If you think it's expensive to hire a professional to do the job, wait until you hire an amateur." Red Adair. nils illegitimus carborundum me, me, me

              A Offline
              A Offline
              AspDotNetDev
              wrote on last edited by
              #6

              Notice anything else about why the code is strange?

              Thou mewling ill-breeding pignut!

              R 1 Reply Last reply
              0
              • A AspDotNetDev

                Notice anything else about why the code is strange?

                Thou mewling ill-breeding pignut!

                R Offline
                R Offline
                R Giskard Reventlov
                wrote on last edited by
                #7

                Yes indeed, but I didn't think that was your point.

                "If you think it's expensive to hire a professional to do the job, wait until you hire an amateur." Red Adair. nils illegitimus carborundum me, me, me

                A 1 Reply Last reply
                0
                • P PIEBALDconsult

                  AspDotNetDev wrote:

                  SqlDbType.Int

                  Soooo many practitioners think that's necessary. :sigh:

                  AspDotNetDev wrote:

                  AddWithValue

                  I never use that; it's not defined by an Interface I use. I use IDbCommand.CreateParameter and IDataParameterCollection.Add.

                  AspDotNetDev wrote:

                  is optional

                  That may be true with the database system you use; it may not be true with all the database systems I use.

                  A Offline
                  A Offline
                  AspDotNetDev
                  wrote on last edited by
                  #8

                  PIEBALDconsult wrote:

                  Soooo many practitioners think that's necessary

                  I find nothing wrong with specifying that. The thing I find funny is the specification that it's an int, and then on the next line passing in a string, which was actually an int to start with (rather than casting to an int).

                  PIEBALDconsult wrote:

                  I never use that

                  The first thing I did was convert it to LINQ. Now the call looks a bit like context.SomeStoredProcedure(someInt). In my next project, that's basically still how I'll do it, only I'll be abstracting away the data source and using Ninject, so where it's actually used it'll look more like dataStore.DoSomething(someInt).

                  PIEBALDconsult wrote:

                  That may be true with the database system you use; it may not be true with all the database systems I use

                  I can't seem to find any documentation regarding the "@" being optional, but much of my code has been written with this fact in mind. SqlClient.SqlCommand knows to automatically prefix "@" for parameter names. I did come across this (see "Working with Parameter Placeholders"), which indicates the parameter naming varies per client. I wonder, then, if removing the prefix and letting the query composer do the prefixing is the best way for the code to be cross-client compatible.

                  Thou mewling ill-breeding pignut!

                  R L 2 Replies Last reply
                  0
                  • R R Giskard Reventlov

                    Yes indeed, but I didn't think that was your point.

                    "If you think it's expensive to hire a professional to do the job, wait until you hire an amateur." Red Adair. nils illegitimus carborundum me, me, me

                    A Offline
                    A Offline
                    AspDotNetDev
                    wrote on last edited by
                    #9

                    My only point is to make fun of as many things as is possible in that code snippet. However, the only real thing wrong with it is converting an integer to a string and then using that string as the value of a parameter that was just explicitly specified to be an integer.

                    Thou mewling ill-breeding pignut!

                    R 1 Reply Last reply
                    0
                    • A AspDotNetDev

                      My only point is to make fun of as many things as is possible in that code snippet. However, the only real thing wrong with it is converting an integer to a string and then using that string as the value of a parameter that was just explicitly specified to be an integer.

                      Thou mewling ill-breeding pignut!

                      R Offline
                      R Offline
                      R Giskard Reventlov
                      wrote on last edited by
                      #10

                      Indeed - but you missed the smiley! I took you seriously! I wouldn't do it in that way (even with correct typing), in any case - too clumsy and not reusable. :)

                      "If you think it's expensive to hire a professional to do the job, wait until you hire an amateur." Red Adair. nils illegitimus carborundum me, me, me

                      1 Reply Last reply
                      0
                      • A AspDotNetDev

                        PIEBALDconsult wrote:

                        Soooo many practitioners think that's necessary

                        I find nothing wrong with specifying that. The thing I find funny is the specification that it's an int, and then on the next line passing in a string, which was actually an int to start with (rather than casting to an int).

                        PIEBALDconsult wrote:

                        I never use that

                        The first thing I did was convert it to LINQ. Now the call looks a bit like context.SomeStoredProcedure(someInt). In my next project, that's basically still how I'll do it, only I'll be abstracting away the data source and using Ninject, so where it's actually used it'll look more like dataStore.DoSomething(someInt).

                        PIEBALDconsult wrote:

                        That may be true with the database system you use; it may not be true with all the database systems I use

                        I can't seem to find any documentation regarding the "@" being optional, but much of my code has been written with this fact in mind. SqlClient.SqlCommand knows to automatically prefix "@" for parameter names. I did come across this (see "Working with Parameter Placeholders"), which indicates the parameter naming varies per client. I wonder, then, if removing the prefix and letting the query composer do the prefixing is the best way for the code to be cross-client compatible.

                        Thou mewling ill-breeding pignut!

                        R Offline
                        R Offline
                        R Giskard Reventlov
                        wrote on last edited by
                        #11

                        AspDotNetDev wrote:

                        I can't seem to find any documentation regarding the "@" being optional, but much of my code has been written with this fact in mind. SqlClient.SqlCommand knows to automatically prefix "@" for parameter names. I did come across this (see "Working with Parameter Placeholders"), which indicates the parameter naming varies per client. I wonder, then, if removing the prefix and letting the query composer do the prefixing is the best way for the code to be cross-client compatible.

                        That's an interesting point.

                        "If you think it's expensive to hire a professional to do the job, wait until you hire an amateur." Red Adair. nils illegitimus carborundum me, me, me

                        1 Reply Last reply
                        0
                        • A AspDotNetDev

                          cmd.Parameters.Add("@SomeID", SqlDbType.Int)
                          cmd.Parameters("@SomeID").Value = Session("SomeID").ToString()

                          It appears this developer didn't have a strong conception of data types, knowledge of AddWithValue, or knowledge that the "@" is optional when specifying parameters. :doh:

                          Thou mewling ill-breeding pignut!

                          J Offline
                          J Offline
                          JeremyBob
                          wrote on last edited by
                          #12

                          AspDotNetDev wrote:

                          "@" is optional when specifying parameters.

                          Actually I believe the "@" symbol only became optional in .Net Framework 2.0 and above, before that it was a requirement. Please don't take my word for that, but its coming from a rusty memory of my own personal experience.

                          1 Reply Last reply
                          0
                          • A AspDotNetDev

                            cmd.Parameters.Add("@SomeID", SqlDbType.Int)
                            cmd.Parameters("@SomeID").Value = Session("SomeID").ToString()

                            It appears this developer didn't have a strong conception of data types, knowledge of AddWithValue, or knowledge that the "@" is optional when specifying parameters. :doh:

                            Thou mewling ill-breeding pignut!

                            L Offline
                            L Offline
                            Luca Leonardo Scorcia
                            wrote on last edited by
                            #13

                            Well, in the codebase I handle I'm enforcing a standard of explicitly stating the parameter type when using ADO.net. Granted, we're autogenerating the DAL using an internal tool, but it helps in a lot of situations. Example: when using a string field, if you specify the length, ADO.net will truncate the parameter to the expected maximum length. Otherwise it gets passed to the underlying db and the query will error out. For us, the former behaviour is preferred. YMMV :)

                            Luca The Price of Freedom is Eternal Vigilance. -- Wing Commander IV En Það Besta Sem Guð Hefur Skapað, Er Nýr Dagur. (But the best thing God has created, is a New Day.) -- Sigur Ròs - Viðrar vel til loftárása

                            1 Reply Last reply
                            0
                            • A AspDotNetDev

                              PIEBALDconsult wrote:

                              Soooo many practitioners think that's necessary

                              I find nothing wrong with specifying that. The thing I find funny is the specification that it's an int, and then on the next line passing in a string, which was actually an int to start with (rather than casting to an int).

                              PIEBALDconsult wrote:

                              I never use that

                              The first thing I did was convert it to LINQ. Now the call looks a bit like context.SomeStoredProcedure(someInt). In my next project, that's basically still how I'll do it, only I'll be abstracting away the data source and using Ninject, so where it's actually used it'll look more like dataStore.DoSomething(someInt).

                              PIEBALDconsult wrote:

                              That may be true with the database system you use; it may not be true with all the database systems I use

                              I can't seem to find any documentation regarding the "@" being optional, but much of my code has been written with this fact in mind. SqlClient.SqlCommand knows to automatically prefix "@" for parameter names. I did come across this (see "Working with Parameter Placeholders"), which indicates the parameter naming varies per client. I wonder, then, if removing the prefix and letting the query composer do the prefixing is the best way for the code to be cross-client compatible.

                              Thou mewling ill-breeding pignut!

                              L Offline
                              L Offline
                              Luca Leonardo Scorcia
                              wrote on last edited by
                              #14

                              I don't think there's a real way to achieve portability on that, it strongly depends on the connector used. ODBC for example uses a positional syntax for parameters, ignores parameter names and uses the '?' char as a parameter placeholder. MS-Access, same thing. The MySQL provider IIRC can be configured both ways, but by default requires the '@' char (or maybe the reverse, it has been a few years since I last used it).

                              Luca The Price of Freedom is Eternal Vigilance. -- Wing Commander IV En Það Besta Sem Guð Hefur Skapað, Er Nýr Dagur. (But the best thing God has created, is a New Day.) -- Sigur Ròs - Viðrar vel til loftárása

                              1 Reply Last reply
                              0
                              • A AspDotNetDev

                                cmd.Parameters.Add("@SomeID", SqlDbType.Int)
                                cmd.Parameters("@SomeID").Value = Session("SomeID").ToString()

                                It appears this developer didn't have a strong conception of data types, knowledge of AddWithValue, or knowledge that the "@" is optional when specifying parameters. :doh:

                                Thou mewling ill-breeding pignut!

                                R Offline
                                R Offline
                                RafagaX
                                wrote on last edited by
                                #15

                                I believe this line:

                                cmd.Parameters("@SomeID").Value = Session("SomeID").ToString()

                                Should be this:

                                cmd.Parameters("@SomeID").Value = Int32.Parse(Session("SomeID").ToString())

                                ;P

                                CEO at: - Rafaga Systems - Para Facturas - Modern Components for the moment...

                                1 Reply Last reply
                                0
                                • J Johnny J

                                  AddWithValue is the best thing since sliced bread (except for bacon)... Makes everything a hell of a lot easier. :thumbsup:

                                  Why can't I be applicable like John? - Me, April 2011
                                  -----
                                  Beidh ceol, caint agus craic againn - Seán Bán Breathnach
                                  -----
                                  Da mihi sis crustum Etruscum cum omnibus in eo!
                                  -----
                                  Just because a thing is new don’t mean that it’s better - Will Rogers, September 4, 1932

                                  O Offline
                                  O Offline
                                  obermd
                                  wrote on last edited by
                                  #16

                                  I quit using AddWithValue when I discovered it eliminated the query optimizer's ability to generate efficient queries. With Stored Procedures it's probably ok, but if the query is embedded in your code it's a performance killer.

                                  1 Reply Last reply
                                  0
                                  • J Johnny J

                                    AddWithValue is the best thing since sliced bread (except for bacon)... Makes everything a hell of a lot easier. :thumbsup:

                                    Why can't I be applicable like John? - Me, April 2011
                                    -----
                                    Beidh ceol, caint agus craic againn - Seán Bán Breathnach
                                    -----
                                    Da mihi sis crustum Etruscum cum omnibus in eo!
                                    -----
                                    Just because a thing is new don’t mean that it’s better - Will Rogers, September 4, 1932

                                    P Offline
                                    P Offline
                                    PIEBALDconsult
                                    wrote on last edited by
                                    #17

                                    Easier isn't always better.

                                    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