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. submitting a ' in a string to sql

submitting a ' in a string to sql

Scheduled Pinned Locked Moved Database
databasehelptutorialquestion
6 Posts 4 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.
  • D Offline
    D Offline
    DxSolo
    wrote on last edited by
    #1

    i have a problem i have a vb6 program which at one point calls a stored procedure that accepts two strings as arguments but there are certain times when on of the strings contains a ' for example execute mystoredproc 'blahblah', 'jame's' this of course generates an error because it thinks that the letters jame is the whole string being submitted so how can i change this so it accepts it

    N S A 3 Replies Last reply
    0
    • D DxSolo

      i have a problem i have a vb6 program which at one point calls a stored procedure that accepts two strings as arguments but there are certain times when on of the strings contains a ' for example execute mystoredproc 'blahblah', 'jame's' this of course generates an error because it thinks that the letters jame is the whole string being submitted so how can i change this so it accepts it

      N Offline
      N Offline
      Nino_1
      wrote on last edited by
      #2

      DX, It sounds like your string in VB is reading the special character as the beginning of a comment. ie: ' Comments go here. I don't know of a workaround for this, I have had the same problem with ASP Pages doing data inserts into SQL Server. Tony

      D 1 Reply Last reply
      0
      • N Nino_1

        DX, It sounds like your string in VB is reading the special character as the beginning of a comment. ie: ' Comments go here. I don't know of a workaround for this, I have had the same problem with ASP Pages doing data inserts into SQL Server. Tony

        D Offline
        D Offline
        DxSolo
        wrote on last edited by
        #3

        no its not vb that has the problem its doesnt think its a comment its when its submitted to the stored procedure that it gets the problem like the stored procedure would accept this execute mystoreproc 'blah','blah' no problem but when you have something like this execute mystoreproc 'blah's','blah' the stored proc will interpet it like this execute mystoreproc 'blah' s ,'blah' because sql uses the ' character to enclose strings that are inserted or otherwise used in queries

        D 1 Reply Last reply
        0
        • D DxSolo

          no its not vb that has the problem its doesnt think its a comment its when its submitted to the stored procedure that it gets the problem like the stored procedure would accept this execute mystoreproc 'blah','blah' no problem but when you have something like this execute mystoreproc 'blah's','blah' the stored proc will interpet it like this execute mystoreproc 'blah' s ,'blah' because sql uses the ' character to enclose strings that are inserted or otherwise used in queries

          D Offline
          D Offline
          DxSolo
          wrote on last edited by
          #4

          i found the solution if you want to use something like 'jake's bigtime' in an insert statement then you would have to simply put two apostrophies and sql will then put the one apostrophe character in the db so the query would look like this insert into test values('jake''s bigtime') and a this query select * from test would return jake's bigtime

          1 Reply Last reply
          0
          • D DxSolo

            i have a problem i have a vb6 program which at one point calls a stored procedure that accepts two strings as arguments but there are certain times when on of the strings contains a ' for example execute mystoredproc 'blahblah', 'jame's' this of course generates an error because it thinks that the letters jame is the whole string being submitted so how can i change this so it accepts it

            S Offline
            S Offline
            Simon_uk
            wrote on last edited by
            #5

            Hello, All you need to do is use two single quotes instead. e.g. 'o''neill'. HTH, Simon. [EDIT] Ahh, I see you posted the correct answer 2 minutes before me! :) [/EDIT]

            1 Reply Last reply
            0
            • D DxSolo

              i have a problem i have a vb6 program which at one point calls a stored procedure that accepts two strings as arguments but there are certain times when on of the strings contains a ' for example execute mystoredproc 'blahblah', 'jame's' this of course generates an error because it thinks that the letters jame is the whole string being submitted so how can i change this so it accepts it

              A Offline
              A Offline
              Arjan Einbu
              wrote on last edited by
              #6

              You should use the parameters collection to submit your parameters to the stored proc. SqlCommand cmd = new SqlCommand("mystoredproc"); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.Add("@param1", "blahblah"); //replace @param1 with the name of your parameter cmd.Parameters.Add("@param2", "jamie's"); //replace @param2 with the name of your parameter cmd.ExecuteNonQuery(); Then the necessary convertions will be done automagically for you.

              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