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. General Programming
  3. Visual Basic
  4. single quotes in vb.net

single quotes in vb.net

Scheduled Pinned Locked Moved Visual Basic
csharpdatabase
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.
  • S Sonia Gupta

    Thank You sir.

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

    While the previous poster did answer your question the result is very poor and potentially dangerous advice. That way SQL Injection Attacks lay. Please read SQL Injection Attacks and Tips on How To Prevent Them[^] and then change your code to use parameterised queries rather than string substitution.


    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
    • A Ali 110

      Hi, Yes u can add quotes just by replacing single quote with double quote. Replacing will add only single quote in database. Example: strName=Xyz's "Insert into table1(Name) Values('" & Replace(strName,"'","''") & "')" Regards Ali Raza

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

      That way SQL Injection Attacks lay - While you did answer the OP's question, the better answer would have been to replace string injection with parameterised queries. That way you don't have to worry about apostrophes in the data and you help prevent SQL Injection Attacks. Please read SQL Injection Attacks and 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
      • R Rupesh Kumar Swami

        yes, you can. try following format str="insert into table1 values (" & """" & var1 & """" & ")" where var1 may contain single & double quotes. hope this helps

        Rupesh Kumar Swami Software Engineer, Integrated Solution, Bikaner (India)

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

        Not another one. Please please please! Will people please learn about SQL Injection Attacks! If you see someone asking a question like this again the best course is to guide them towards parameterised queries as it helps prevent SQL Injection Attacks. Any answer that still involved injecting data in to a SQL String is potentially dangerous. Please read SQL Injection Attacks and 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

        L D P G 4 Replies Last reply
        0
        • C Colin Angus Mackay

          Not another one. Please please please! Will people please learn about SQL Injection Attacks! If you see someone asking a question like this again the best course is to guide them towards parameterised queries as it helps prevent SQL Injection Attacks. Any answer that still involved injecting data in to a SQL String is potentially dangerous. Please read SQL Injection Attacks and 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

          L Offline
          L Offline
          leckey 0
          wrote on last edited by
          #9

          Yes, you don't want an angry Scot after you! ;P

          __________________ Bob is my homeboy.

          1 Reply Last reply
          0
          • C Colin Angus Mackay

            Not another one. Please please please! Will people please learn about SQL Injection Attacks! If you see someone asking a question like this again the best course is to guide them towards parameterised queries as it helps prevent SQL Injection Attacks. Any answer that still involved injecting data in to a SQL String is potentially dangerous. Please read SQL Injection Attacks and 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

            D Offline
            D Offline
            Dave Kreskowiak
            wrote on last edited by
            #10

            "If it's not Scottish - It's CR****P!"

            A guide to posting questions on CodeProject[^]
            Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                 2006, 2007

            C 1 Reply Last reply
            0
            • R Rupesh Kumar Swami

              yes, you can. try following format str="insert into table1 values (" & """" & var1 & """" & ")" where var1 may contain single & double quotes. hope this helps

              Rupesh Kumar Swami Software Engineer, Integrated Solution, Bikaner (India)

              A Offline
              A Offline
              advansis
              wrote on last edited by
              #11

              Hi, I don't know if could exist other problems, but I've resolved the problem doubling the apostrophes: str="INSERT INTO Table1 VALUES(" & Replace(Var1,"'","''") & ")" In this way, SQL injection by writing apostrophes is not possible (or it is anyway ?)

              Peace!

              C 1 Reply Last reply
              0
              • A advansis

                Hi, I don't know if could exist other problems, but I've resolved the problem doubling the apostrophes: str="INSERT INTO Table1 VALUES(" & Replace(Var1,"'","''") & ")" In this way, SQL injection by writing apostrophes is not possible (or it is anyway ?)

                Peace!

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

                But you are still injecting values into the SQL command. If you are injecting values in to the SQL command then attacks are possible. That's why it is called a SQL injection attack.


                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

                P 1 Reply Last reply
                0
                • D Dave Kreskowiak

                  "If it's not Scottish - It's CR****P!"

                  A guide to posting questions on CodeProject[^]
                  Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                       2006, 2007

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

                  Dave Kreskowiak wrote:

                  "If it's not Scottish - It's CR****P!"

                  Gaun yersel there, Big Yin.


                  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

                  D 1 Reply Last reply
                  0
                  • C Colin Angus Mackay

                    Dave Kreskowiak wrote:

                    "If it's not Scottish - It's CR****P!"

                    Gaun yersel there, Big Yin.


                    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

                    D Offline
                    D Offline
                    Dave Kreskowiak
                    wrote on last edited by
                    #14

                    Colin Angus Mackay wrote:

                    Gaun yersel there, Big Yin.

                    It took me a minute to figure that one out! :-D Thank you!

                    A guide to posting questions on CodeProject[^]
                    Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                         2006, 2007

                    1 Reply Last reply
                    0
                    • C Colin Angus Mackay

                      Not another one. Please please please! Will people please learn about SQL Injection Attacks! If you see someone asking a question like this again the best course is to guide them towards parameterised queries as it helps prevent SQL Injection Attacks. Any answer that still involved injecting data in to a SQL String is potentially dangerous. Please read SQL Injection Attacks and 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

                      P Offline
                      P Offline
                      Paul Conrad
                      wrote on last edited by
                      #15

                      Colin Angus Mackay wrote:

                      Please please please! Will people please learn about SQL Injection Attacks!

                      :laugh: No kidding...

                      1 Reply Last reply
                      0
                      • C Colin Angus Mackay

                        But you are still injecting values into the SQL command. If you are injecting values in to the SQL command then attacks are possible. That's why it is called a SQL injection attack.


                        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

                        P Offline
                        P Offline
                        Paul Conrad
                        wrote on last edited by
                        #16

                        It isn't that hard to add in code to prevent the injection attacks, if I may add :rolleyes:

                        1 Reply Last reply
                        0
                        • C Colin Angus Mackay

                          Not another one. Please please please! Will people please learn about SQL Injection Attacks! If you see someone asking a question like this again the best course is to guide them towards parameterised queries as it helps prevent SQL Injection Attacks. Any answer that still involved injecting data in to a SQL String is potentially dangerous. Please read SQL Injection Attacks and 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

                          G Offline
                          G Offline
                          Guffa
                          wrote on last edited by
                          #17

                          I think that you should read what you are linking to yourself. ;) If the values are encoded correctly, there is no problem with concatenating string to create an SQL query. It's only if you do it wrong that the code is subject to SQL injections. Doing it right is not trivial, though, and the methods presented in this thread is for example not at all suitable if you are using an MySQL database. To encode a string for MySQL you would instead replace "\" with "\\", then replace "'" with "\'". So, using parameterised queries is good advice. :) It's not, however, the only way to protect the code against SQL injections.

                          --- single minded; short sighted; long gone;

                          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