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. Replacing ' in a string

Replacing ' in a string

Scheduled Pinned Locked Moved Database
databasecsharpmysqlhelpquestion
6 Posts 5 Posters 2 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
    phimix
    wrote on last edited by
    #1

    Hi, I have a problem with strings containing ' (that is the character '). The strings need to be saved to a MySQL database, but the MySQLCommand doesn't accept the ' character. It only accept it as \' Problem is I can't make C# replace it properly (2nd line in the code):

    string myString = "The world's largest program";
    myString = myString.Replace("'", "\'");

    string sql = "UPDATE myTable SET myColumn = '" + myString + "'";
    MySQLCommand cmd = new MySQLCommand(sql, con);
    int result = Convert.ToInt32(cmd.ExecuteNonQuery());

    Any ideas? Thanks a lot.

    L J L R 4 Replies Last reply
    0
    • P phimix

      Hi, I have a problem with strings containing ' (that is the character '). The strings need to be saved to a MySQL database, but the MySQLCommand doesn't accept the ' character. It only accept it as \' Problem is I can't make C# replace it properly (2nd line in the code):

      string myString = "The world's largest program";
      myString = myString.Replace("'", "\'");

      string sql = "UPDATE myTable SET myColumn = '" + myString + "'";
      MySQLCommand cmd = new MySQLCommand(sql, con);
      int result = Convert.ToInt32(cmd.ExecuteNonQuery());

      Any ideas? Thanks a lot.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      You can do it in the initial string thus:

      string myString = "The world\'s largest program";

      or in your replace call thus:

      myString = myString.Replace("'", "\\'");

      In case 1 you need to escape the single quote with a backslash, and in case 2 you need to escape the backslash.

      I must get a clever new signature for 2011.

      P 1 Reply Last reply
      0
      • P phimix

        Hi, I have a problem with strings containing ' (that is the character '). The strings need to be saved to a MySQL database, but the MySQLCommand doesn't accept the ' character. It only accept it as \' Problem is I can't make C# replace it properly (2nd line in the code):

        string myString = "The world's largest program";
        myString = myString.Replace("'", "\'");

        string sql = "UPDATE myTable SET myColumn = '" + myString + "'";
        MySQLCommand cmd = new MySQLCommand(sql, con);
        int result = Convert.ToInt32(cmd.ExecuteNonQuery());

        Any ideas? Thanks a lot.

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

        Use parameters instead.

        List of common misconceptions

        1 Reply Last reply
        0
        • L Lost User

          You can do it in the initial string thus:

          string myString = "The world\'s largest program";

          or in your replace call thus:

          myString = myString.Replace("'", "\\'");

          In case 1 you need to escape the single quote with a backslash, and in case 2 you need to escape the backslash.

          I must get a clever new signature for 2011.

          P Offline
          P Offline
          phimix
          wrote on last edited by
          #4

          It's case 2 we need to go for (the myString is just an example that I created for my question, in reality it's a much more complex string) But case 2 doesn't seem to work:

          string myString = "The world's largest program";
          myString = myString.Replace("'", "\\'");

          string sql = "UPDATE myTable SET myColumn = '" + myString + "'";
          MySQLCommand cmd = new MySQLCommand(sql, con);
          int result = Convert.ToInt32(cmd.ExecuteNonQuery());

          Gives us the SQL string:

          UPDATE myTable SET myColumn = 'The world\\'s largest program'

          It has to end up like this to work (I have checked it by typing it in the program SQLyog (which give direct SQL access to a MySQL database):

          UPDATE myTable SET myColumn = 'The world\'s largest program'

          1 Reply Last reply
          0
          • P phimix

            Hi, I have a problem with strings containing ' (that is the character '). The strings need to be saved to a MySQL database, but the MySQLCommand doesn't accept the ' character. It only accept it as \' Problem is I can't make C# replace it properly (2nd line in the code):

            string myString = "The world's largest program";
            myString = myString.Replace("'", "\'");

            string sql = "UPDATE myTable SET myColumn = '" + myString + "'";
            MySQLCommand cmd = new MySQLCommand(sql, con);
            int result = Convert.ToInt32(cmd.ExecuteNonQuery());

            Any ideas? Thanks a lot.

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #5

            Once you got the single quote in the database, it does not make sense to replace it by some escape sequence; escape sequences are used in a programming language (such as SQL), not in the data itself. So use \' in the queries when you have to. And it is always better to use SqlParameter, you won't need escape sequences then at all. :)

            Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

            Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

            1 Reply Last reply
            0
            • P phimix

              Hi, I have a problem with strings containing ' (that is the character '). The strings need to be saved to a MySQL database, but the MySQLCommand doesn't accept the ' character. It only accept it as \' Problem is I can't make C# replace it properly (2nd line in the code):

              string myString = "The world's largest program";
              myString = myString.Replace("'", "\'");

              string sql = "UPDATE myTable SET myColumn = '" + myString + "'";
              MySQLCommand cmd = new MySQLCommand(sql, con);
              int result = Convert.ToInt32(cmd.ExecuteNonQuery());

              Any ideas? Thanks a lot.

              R Offline
              R Offline
              RyanEK
              wrote on last edited by
              #6

              Use parameters instead to prevent SQL Injection

              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