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. Database & SysAdmin
  3. Database
  4. Escape Sequences....

Escape Sequences....

Scheduled Pinned Locked Moved Database
csharpdatabasehelpquestionoop
10 Posts 5 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.
  • P Offline
    P Offline
    Polite Programmer
    wrote on last edited by
    #1

    Hello! I am developing a .NET 2.0 solution (C#) using ADO.NET I have a question regarding the escape sequences in SQL. Consider the following query SELECT * FROM ClauseTranslations WHERE Clause LIKE 'trainee's cost' Now when executed, this query generates an exception. Very well. The problem with with the single quote symbol. so what I do, I write like this: SELECT * FROM ClauseTranslations WHERE Clause LIKE 'trainee\'s cost' But this still gives error... What the way out? Regards, Mohsin

    Polite Programmer


    More Object Oriented then C#

    P G C R 4 Replies Last reply
    0
    • P Polite Programmer

      Hello! I am developing a .NET 2.0 solution (C#) using ADO.NET I have a question regarding the escape sequences in SQL. Consider the following query SELECT * FROM ClauseTranslations WHERE Clause LIKE 'trainee's cost' Now when executed, this query generates an exception. Very well. The problem with with the single quote symbol. so what I do, I write like this: SELECT * FROM ClauseTranslations WHERE Clause LIKE 'trainee\'s cost' But this still gives error... What the way out? Regards, Mohsin

      Polite Programmer


      More Object Oriented then C#

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

      replace with -SELECT * FROM ClauseTranslations WHERE Clause LIKE 'trainee**''**s cost' the error occured because of 's so,u better replace ''s instead of 's

      1 Reply Last reply
      0
      • P Polite Programmer

        Hello! I am developing a .NET 2.0 solution (C#) using ADO.NET I have a question regarding the escape sequences in SQL. Consider the following query SELECT * FROM ClauseTranslations WHERE Clause LIKE 'trainee's cost' Now when executed, this query generates an exception. Very well. The problem with with the single quote symbol. so what I do, I write like this: SELECT * FROM ClauseTranslations WHERE Clause LIKE 'trainee\'s cost' But this still gives error... What the way out? Regards, Mohsin

        Polite Programmer


        More Object Oriented then C#

        G Offline
        G Offline
        Giorgi Dalakishvili
        wrote on last edited by
        #3

        when executing a query from c# application it is better to use parametrized query and there won't be any problems with escape characters.

        my articles

        1 Reply Last reply
        0
        • P Polite Programmer

          Hello! I am developing a .NET 2.0 solution (C#) using ADO.NET I have a question regarding the escape sequences in SQL. Consider the following query SELECT * FROM ClauseTranslations WHERE Clause LIKE 'trainee's cost' Now when executed, this query generates an exception. Very well. The problem with with the single quote symbol. so what I do, I write like this: SELECT * FROM ClauseTranslations WHERE Clause LIKE 'trainee\'s cost' But this still gives error... What the way out? Regards, Mohsin

          Polite Programmer


          More Object Oriented then C#

          C Offline
          C Offline
          Colin Angus Mackay
          wrote on last edited by
          #4

          You should use parameterised queries instead of using escape sequences becuase the need to escape characters means that you are more likely to miss something and allow a SQL Injection Attack to take place. Please read SQL Injection Attacks and Some Tips on How to Prevent Them[^]


          Upcoming events: * Glasgow: SQL Server 2005 - XML and XML Query Plans, Mock Objects, SQL Server Reporting Services... Never write for other people. Write for yourself, because you have a passion for it. -- Marc Clifton My website

          1 Reply Last reply
          0
          • P Polite Programmer

            Hello! I am developing a .NET 2.0 solution (C#) using ADO.NET I have a question regarding the escape sequences in SQL. Consider the following query SELECT * FROM ClauseTranslations WHERE Clause LIKE 'trainee's cost' Now when executed, this query generates an exception. Very well. The problem with with the single quote symbol. so what I do, I write like this: SELECT * FROM ClauseTranslations WHERE Clause LIKE 'trainee\'s cost' But this still gives error... What the way out? Regards, Mohsin

            Polite Programmer


            More Object Oriented then C#

            R Offline
            R Offline
            Ron Savage
            wrote on last edited by
            #5

            Hi Mohsin, I don't think SQL syntax supports escape characters like that. To use an embedded single quote in a query like your example, you put two single quotes in the string: SELECT * FROM ClauseTranslations WHERE Clause LIKE 'trainee''s cost'

            Ron

            C 1 Reply Last reply
            0
            • R Ron Savage

              Hi Mohsin, I don't think SQL syntax supports escape characters like that. To use an embedded single quote in a query like your example, you put two single quotes in the string: SELECT * FROM ClauseTranslations WHERE Clause LIKE 'trainee''s cost'

              Ron

              C Offline
              C Offline
              Colin Angus Mackay
              wrote on last edited by
              #6

              That way SQL Injection Attacks lie.... Rather than escape characters, use parameterised queries.


              Upcoming events: * Glasgow: SQL Server 2005 - XML and XML Query Plans, Mock Objects, SQL Server Reporting Services... Never write for other people. Write for yourself, because you have a passion for it. -- Marc Clifton My website

              R 1 Reply Last reply
              0
              • C Colin Angus Mackay

                That way SQL Injection Attacks lie.... Rather than escape characters, use parameterised queries.


                Upcoming events: * Glasgow: SQL Server 2005 - XML and XML Query Plans, Mock Objects, SQL Server Reporting Services... Never write for other people. Write for yourself, because you have a passion for it. -- Marc Clifton My website

                R Offline
                R Offline
                Ron Savage
                wrote on last edited by
                #7

                Good advice, but it doesn't answer his question. *If* he is accepting user input to build the query, then SQL Injection attacks are an issue. If the entire query as written is part of his internal code, there is no danger.

                Ron

                C 1 Reply Last reply
                0
                • R Ron Savage

                  Good advice, but it doesn't answer his question. *If* he is accepting user input to build the query, then SQL Injection attacks are an issue. If the entire query as written is part of his internal code, there is no danger.

                  Ron

                  C Offline
                  C Offline
                  Colin Angus Mackay
                  wrote on last edited by
                  #8

                  Ron Savage wrote:

                  If the entire query as written is part of his internal code, there is no danger.

                  That's untrue. There is the possibility of a Second Order Attack. This is where supposedly clensed data that is already sitting in the database can be used to form an attack. All the data used is internal to the system at the time the SQL is formed, but the threat is just as real.


                  Upcoming events: * Glasgow: SQL Server 2005 - XML and XML Query Plans, Mock Objects, SQL Server Reporting Services... Never write for other people. Write for yourself, because you have a passion for it. -- Marc Clifton My website

                  R 1 Reply Last reply
                  0
                  • C Colin Angus Mackay

                    Ron Savage wrote:

                    If the entire query as written is part of his internal code, there is no danger.

                    That's untrue. There is the possibility of a Second Order Attack. This is where supposedly clensed data that is already sitting in the database can be used to form an attack. All the data used is internal to the system at the time the SQL is formed, but the threat is just as real.


                    Upcoming events: * Glasgow: SQL Server 2005 - XML and XML Query Plans, Mock Objects, SQL Server Reporting Services... Never write for other people. Write for yourself, because you have a passion for it. -- Marc Clifton My website

                    R Offline
                    R Offline
                    Ron Savage
                    wrote on last edited by
                    #9

                    Colin Angus Mackay wrote:

                    There is the possibility of a Second Order Attack. This is where supposedly clensed data that is already sitting in the database can be used to form an attack. All the data used is internal to the system at the time the SQL is formed, but the threat is just as real.

                    Another excellent bit of advice, yet again completely outside the context of the discussion. Neither the original question nor my response involved building a query from any external data. Without the inclusion of external text that may at some point have been entered by an end user of the program - SQL Injection attacks are not an issue.

                    Ron

                    C 1 Reply Last reply
                    0
                    • R Ron Savage

                      Colin Angus Mackay wrote:

                      There is the possibility of a Second Order Attack. This is where supposedly clensed data that is already sitting in the database can be used to form an attack. All the data used is internal to the system at the time the SQL is formed, but the threat is just as real.

                      Another excellent bit of advice, yet again completely outside the context of the discussion. Neither the original question nor my response involved building a query from any external data. Without the inclusion of external text that may at some point have been entered by an end user of the program - SQL Injection attacks are not an issue.

                      Ron

                      C Offline
                      C Offline
                      Colin Angus Mackay
                      wrote on last edited by
                      #10

                      Ron Savage wrote:

                      Another excellent bit of advice, yet again completely outside the context of the discussion.

                      Is it?!

                      Ron Savage wrote:

                      Neither the original question nor my response involved building a query from any external data.

                      Who's to say where the data came from? The OP certainly didn't. So any discussion on the matter is pure speculation without a definitive answer from the OP. As such, it becomes important that the issue of SQL Injection is raised... just in case.

                      Ron Savage wrote:

                      Without the inclusion of external text that may at some point have been entered by an end user of the program - SQL Injection attacks are not an issue.

                      Hardcoding fully formed SQL statements into one's program is hardly common. The more common scenario is that the statement contains some variable data. As such, in case the OP is giving a simplified example (which is common) it "yet again" becomes important to raise the issue of SQL Injection Attacks.


                      Upcoming events: * Glasgow: SQL Server 2005 - XML and XML Query Plans, Mock Objects, SQL Server Reporting Services... Never write for other people. Write for yourself, because you have a passion for it. -- Marc Clifton My website

                      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