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. String.Format???

String.Format???

Scheduled Pinned Locked Moved The Weird and The Wonderful
rubydatabasehelpquestionlearning
54 Posts 16 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.
  • L Lost User

    Using SQL in code is generally considered bad practice. It can raise maintenance issues and can be exposed to SQL injection attacks to name just a few reasons.

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

    tinko101 wrote:

    SQL injection attacks

    That's only due to this particular command not using parameters.

    tinko101 wrote:

    raise maintenance issues

    Stored procedures raise maintenance issues too. And how will you execute your stored procedure from code without having SQL in your code?

    1 Reply Last reply
    0
    • J Jorgen Andersson

      How would you add the parameter for a query like: SELECT * FROM Table WHERE ID IN (123,124,125);? Lists are a bit tougher to handle in a proper way...

      "When did ignorance become a point of view" - Dilbert

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

      Personally I wouldn't. I wouldn't use IN at all, I'd find a way to have a table on which to JOIN instead. The statement you present is a symptom of a poorly implemented system.

      D O J 3 Replies Last reply
      0
      • O oggenok64

        Why is SQL in code such a particular horror?

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

        It isn't; pay no attention to the stored procedure fan boys.

        O 1 Reply Last reply
        0
        • L Lost User

          I agree, that might be the case, but the fact still remains (as CDP1802 noted), that having SQL statement in code formatted with parameters that might come from e.g. UI text-boxes, represents great vulnerability to SQL injection attacks. Otherwise I understand that sometimes there is no other way, nevertheless in-code SQL can be used wisely or not.

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

          tinko101 wrote:

          in-code SQL can be used wisely or not

          Exactly.

          tinko101 wrote:

          SQL injection attacks.

          Parameterization handles that regardless of where the SQL statement is stored.

          modified on Friday, July 9, 2010 12:02 PM

          1 Reply Last reply
          0
          • P PIEBALDconsult

            It isn't; pay no attention to the stored procedure fan boys.

            O Offline
            O Offline
            oggenok64
            wrote on last edited by
            #14

            While we are at it, could someone please explain to me why inline SQL might be more vulnerable to SQL injection than a stored proc. Having thought about it all afternoon, i must admit that i just can't see it. Am i blind?

            P 1 Reply Last reply
            0
            • O oggenok64

              While we are at it, could someone please explain to me why inline SQL might be more vulnerable to SQL injection than a stored proc. Having thought about it all afternoon, i must admit that i just can't see it. Am i blind?

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

              It really isn't. However, when using stored procedures, you are pretty much forced to use parameters. When using embedded SQL, you have the option, and they who don't know about parameters or are too lazy to bother, wind up with bad code. Basically, whichever way you store your SQL statements, use parameters. A properly-written Data Access Layer will hide the details anyway.

              S 1 Reply Last reply
              0
              • P PIEBALDconsult

                It really isn't. However, when using stored procedures, you are pretty much forced to use parameters. When using embedded SQL, you have the option, and they who don't know about parameters or are too lazy to bother, wind up with bad code. Basically, whichever way you store your SQL statements, use parameters. A properly-written Data Access Layer will hide the details anyway.

                S Offline
                S Offline
                Steve Wellens
                wrote on last edited by
                #16

                Here's a good article about SQL Injection: http://www.mikesdotnetting.com/Article/113/Preventing-SQL-Injection-in-ASP.NET[^]

                Steve Wellens

                P 1 Reply Last reply
                0
                • S Steve Wellens

                  Here's a good article about SQL Injection: http://www.mikesdotnetting.com/Article/113/Preventing-SQL-Injection-in-ASP.NET[^]

                  Steve Wellens

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

                  Yes, that's good.

                  1 Reply Last reply
                  0
                  • P PIEBALDconsult

                    Personally I wouldn't. I wouldn't use IN at all, I'd find a way to have a table on which to JOIN instead. The statement you present is a symptom of a poorly implemented system.

                    D Offline
                    D Offline
                    David Skelly
                    wrote on last edited by
                    #18

                    There was a thread on here recently discussing this, comparing IN, EXISTS and JOIN. I can't find it now, but if I remember correctly there was a link on there to a blog from one of the SQL Server tech-heads that explained why and how the three are not interchangeable.

                    P 1 Reply Last reply
                    0
                    • N Not Active

                      While working with a client to help them clean up their code I found this little gem. They absolutely never knew string.Format existed :wtf:

                      string sql = "select * from table where id={0} and date={1}";
                      string cmdText = sql.replace("{0}", id.Tostring())
                      .replace("{1}", DateTime.Now.ToShortDateString());


                      I know the language. I've read a book. - _Madmatt

                      S Offline
                      S Offline
                      Single Step Debugger
                      wrote on last edited by
                      #19

                      This looks like a direct port from MFC C++ codebase to .NET and C# from someone not quite familiar with the C#.

                      The narrow specialist in the broad sense of the word is a complete idiot in the narrow sense of the word. Advertise here – minimum three posts per day are guaranteed.

                      1 Reply Last reply
                      0
                      • D David Skelly

                        There was a thread on here recently discussing this, comparing IN, EXISTS and JOIN. I can't find it now, but if I remember correctly there was a link on there to a blog from one of the SQL Server tech-heads that explained why and how the three are not interchangeable.

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

                        That might be an interesting read; I'll take a look to see if I can find it. On the other hand, JOIN can do what IN and EXISTS can do, but IN and EXISTS can't do what JOIN does. P.S. I just searched the general database forum back to May 1 and didn't find it.

                        modified on Thursday, August 5, 2010 12:02 AM

                        J 1 Reply Last reply
                        0
                        • P PIEBALDconsult

                          Personally I wouldn't. I wouldn't use IN at all, I'd find a way to have a table on which to JOIN instead. The statement you present is a symptom of a poorly implemented system.

                          O Offline
                          O Offline
                          oggenok64
                          wrote on last edited by
                          #21

                          Using an SQL IN-clause is definetely not a design flaw. Forcing everything into JOIN's is on the other hand an odd self-imposed hinderence.

                          P 1 Reply Last reply
                          0
                          • J Jorgen Andersson

                            How would you add the parameter for a query like: SELECT * FROM Table WHERE ID IN (123,124,125);? Lists are a bit tougher to handle in a proper way...

                            "When did ignorance become a point of view" - Dilbert

                            J Offline
                            J Offline
                            Jeremy Hutchinson
                            wrote on last edited by
                            #22

                            I would imagine this would work:

                            string query = "select * from table where ID in (@value1, @value2, @value3)";
                            SqlCommand cmd = new SqlCommand(query);
                            cmd.Parameters.Add("@value1", 123);
                            cmd.Parameters.Add("@value2", 124);
                            cmd.Parameters.Add("@value3", 125);

                            If not, this surely would:

                            string query = "select * from table where (ID = @value1 or ID = @value2 or ID = @value3)";
                            SqlCommand cmd = new SqlCommand(query);
                            cmd.Parameters.Add("@value1", 123);
                            cmd.Parameters.Add("@value2", 124);
                            cmd.Parameters.Add("@value3", 125);

                            J 1 Reply Last reply
                            0
                            • O oggenok64

                              Using an SQL IN-clause is definetely not a design flaw. Forcing everything into JOIN's is on the other hand an odd self-imposed hinderence.

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

                              While it is true that one should "use the right tool for the right job", I have never used EXISTS (I have an Oracle background), and I have not used IN/NOT IN for many years, and never with SQL Server. JOIN tends to scale better -- you may have an IN, EXISTS, or even a BETWEEN that has to be converted to a JOIN as the project becomes more complex; using a JOIN to begin with eases such maintenance. JOIN allows you to configure a system by maintaining a table rather than modifying the code. As with Jörgen's post, an IN with hard-coded values I especially discourage; they reek of the "magic numbers" code smell. A subquery on some table would at least improve maintainability.

                              J 1 Reply Last reply
                              0
                              • L Lost User

                                I agree, that might be the case, but the fact still remains (as CDP1802 noted), that having SQL statement in code formatted with parameters that might come from e.g. UI text-boxes, represents great vulnerability to SQL injection attacks. Otherwise I understand that sometimes there is no other way, nevertheless in-code SQL can be used wisely or not.

                                T Offline
                                T Offline
                                T M Gray
                                wrote on last edited by
                                #24

                                Did you look at the code in the OP? You can't do SQL injection if your parameter data is strongly typed Int32 and DateTime values. If it was a string that's a different story, but if you are doing extra code to make sure no one is slipping SQL keywords into your ints then you are wasting a lot of time way overarchitecting.

                                L H 2 Replies Last reply
                                0
                                • O oggenok64

                                  Admittedly a SP would be much a nicer solution, but in real life you may have to work with databases where you are nowhere near getting authorized to implement a SP. Think of implementing a reporting system for at large financial institution as a consultant. What do you think their reply would be if you came saying "I need a dozen new stored procedures in your central DB2-database"? The polite answers would be something along the lines of "I'm sorry but that won't be possible", "Are you quite sure this is needed?" etc, etc. The impolite answer would be to find someone else to do the job.

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

                                  Søren Turin wrote:

                                  a nicer solution

                                  Not for a simple SELECT.

                                  1 Reply Last reply
                                  0
                                  • P PIEBALDconsult

                                    That might be an interesting read; I'll take a look to see if I can find it. On the other hand, JOIN can do what IN and EXISTS can do, but IN and EXISTS can't do what JOIN does. P.S. I just searched the general database forum back to May 1 and didn't find it.

                                    modified on Thursday, August 5, 2010 12:02 AM

                                    J Offline
                                    J Offline
                                    Jorgen Andersson
                                    wrote on last edited by
                                    #26

                                    That's because it wasn't in the general database forum. You'll find it here.[^]

                                    "When did ignorance become a point of view" - Dilbert

                                    P 1 Reply Last reply
                                    0
                                    • P PIEBALDconsult

                                      Personally I wouldn't. I wouldn't use IN at all, I'd find a way to have a table on which to JOIN instead. The statement you present is a symptom of a poorly implemented system.

                                      J Offline
                                      J Offline
                                      Jorgen Andersson
                                      wrote on last edited by
                                      #27

                                      PIEBALDconsult wrote:

                                      Personally I wouldn't. I wouldn't use IN at all, I'd find a way to have a table on which to JOIN instead.

                                      A JOIN is a poor substitute for a NOT IN or NOT EXISTS. And they don't handle nulls the same way.

                                      PIEBALDconsult wrote:

                                      The statement you present is a symptom of a poorly implemented system.

                                      Agreed, but sometimes that's what you have. :sigh:

                                      "When did ignorance become a point of view" - Dilbert

                                      P 1 Reply Last reply
                                      0
                                      • J Jorgen Andersson

                                        That's because it wasn't in the general database forum. You'll find it here.[^]

                                        "When did ignorance become a point of view" - Dilbert

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

                                        Ah, good, thanks.

                                        1 Reply Last reply
                                        0
                                        • J Jorgen Andersson

                                          PIEBALDconsult wrote:

                                          Personally I wouldn't. I wouldn't use IN at all, I'd find a way to have a table on which to JOIN instead.

                                          A JOIN is a poor substitute for a NOT IN or NOT EXISTS. And they don't handle nulls the same way.

                                          PIEBALDconsult wrote:

                                          The statement you present is a symptom of a poorly implemented system.

                                          Agreed, but sometimes that's what you have. :sigh:

                                          "When did ignorance become a point of view" - Dilbert

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

                                          But your example uses IN, not NOT IN. :-D

                                          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