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. C#
  4. SQL Escape Sequences

SQL Escape Sequences

Scheduled Pinned Locked Moved C#
csharpdatabasealgorithmshelptutorial
7 Posts 5 Posters 1 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.
  • K Offline
    K Offline
    Kannan Kalyanaraman
    wrote on last edited by
    #1

    I'm constructing a search string based on input from user in a web application. This search string basically translates to a search criteria and is assigned to a RowFilter on a dataview. My code throws an exception when my search strings contains quotes and other SQL characters which might invalidate the search criteria. How do I make sure that this doesnt happen, ie. how to avoid sql escape characters from my search string. Is there a method in .net which will help me. I tried google, searching for SQL escape characters but I couldnt find anything relevant to what i'm looking for. Also note that the filtering is done on a dataview which is already on cache. Thanks, Kannan

    W A H V 4 Replies Last reply
    0
    • K Kannan Kalyanaraman

      I'm constructing a search string based on input from user in a web application. This search string basically translates to a search criteria and is assigned to a RowFilter on a dataview. My code throws an exception when my search strings contains quotes and other SQL characters which might invalidate the search criteria. How do I make sure that this doesnt happen, ie. how to avoid sql escape characters from my search string. Is there a method in .net which will help me. I tried google, searching for SQL escape characters but I couldnt find anything relevant to what i'm looking for. Also note that the filtering is done on a dataview which is already on cache. Thanks, Kannan

      W Offline
      W Offline
      Werdna
      wrote on last edited by
      #2

      I think the only problem is with ' and you can just replace it with '' (not a quote, but 2 apostrophies).

      1 Reply Last reply
      0
      • K Kannan Kalyanaraman

        I'm constructing a search string based on input from user in a web application. This search string basically translates to a search criteria and is assigned to a RowFilter on a dataview. My code throws an exception when my search strings contains quotes and other SQL characters which might invalidate the search criteria. How do I make sure that this doesnt happen, ie. how to avoid sql escape characters from my search string. Is there a method in .net which will help me. I tried google, searching for SQL escape characters but I couldnt find anything relevant to what i'm looking for. Also note that the filtering is done on a dataview which is already on cache. Thanks, Kannan

        A Offline
        A Offline
        Alvaro Mendez
        wrote on last edited by
        #3

        I have this method in one of my utility classes:

        public static string ToSQL(object value)
        {
        if (value == null)
        return "NULL";

        if (value is string)
        	return "'" + value.ToString().Replace("'", "''") + "'";
        			
        return value.ToString();
        

        }

        It's handy for SQL search criteria. Regards, Alvaro


        Give a man a fish, he owes you one fish. Teach a man to fish, you give up your monopoly on fisheries.

        1 Reply Last reply
        0
        • K Kannan Kalyanaraman

          I'm constructing a search string based on input from user in a web application. This search string basically translates to a search criteria and is assigned to a RowFilter on a dataview. My code throws an exception when my search strings contains quotes and other SQL characters which might invalidate the search criteria. How do I make sure that this doesnt happen, ie. how to avoid sql escape characters from my search string. Is there a method in .net which will help me. I tried google, searching for SQL escape characters but I couldnt find anything relevant to what i'm looking for. Also note that the filtering is done on a dataview which is already on cache. Thanks, Kannan

          H Offline
          H Offline
          Heath Stewart
          wrote on last edited by
          #4

          The easiest way to handle this is used parameterized SQL expressions in ADO.NET. For example, if you use a SqlCommand, use the SqlParameters in the SqlCommand.Parameters (the property documentation includes an example). Constructing the SQL string without the parameters is straight forward enough since you have total control over it. Using parameters for the values (for an INSERT, UPDATE, DELETE, etc.) will eliminate the common problems (trying to inject another statement) while taking care of escaping quotes for you. This is by far a better and safer alternative. The days of concatenating strings to form a SQL expression are (or at least should be) over.

          Microsoft MVP, Visual C# My Articles

          K 1 Reply Last reply
          0
          • H Heath Stewart

            The easiest way to handle this is used parameterized SQL expressions in ADO.NET. For example, if you use a SqlCommand, use the SqlParameters in the SqlCommand.Parameters (the property documentation includes an example). Constructing the SQL string without the parameters is straight forward enough since you have total control over it. Using parameters for the values (for an INSERT, UPDATE, DELETE, etc.) will eliminate the common problems (trying to inject another statement) while taking care of escaping quotes for you. This is by far a better and safer alternative. The days of concatenating strings to form a SQL expression are (or at least should be) over.

            Microsoft MVP, Visual C# My Articles

            K Offline
            K Offline
            Kannan Kalyanaraman
            wrote on last edited by
            #5

            Thanks for the tip, I was aware of this, but was wondering how one could use them for search criteria as that of a RowFilter which expects a SQL string. Thanks, Kannan

            H 1 Reply Last reply
            0
            • K Kannan Kalyanaraman

              Thanks for the tip, I was aware of this, but was wondering how one could use them for search criteria as that of a RowFilter which expects a SQL string. Thanks, Kannan

              H Offline
              H Offline
              Heath Stewart
              wrote on last edited by
              #6

              You're on your own, there. There is a good side, though: users can't cause an injection of an invalid SQL statement in the RowFilter property, thus - potentially - whiping out your entire database. An Exception would be thrown if anything is invalid. So, while there's nothing really to help you with that (just good ol' string parsing), there's not much harm in not checking for every possible exploit.

              Microsoft MVP, Visual C# My Articles

              1 Reply Last reply
              0
              • K Kannan Kalyanaraman

                I'm constructing a search string based on input from user in a web application. This search string basically translates to a search criteria and is assigned to a RowFilter on a dataview. My code throws an exception when my search strings contains quotes and other SQL characters which might invalidate the search criteria. How do I make sure that this doesnt happen, ie. how to avoid sql escape characters from my search string. Is there a method in .net which will help me. I tried google, searching for SQL escape characters but I couldnt find anything relevant to what i'm looking for. Also note that the filtering is done on a dataview which is already on cache. Thanks, Kannan

                V Offline
                V Offline
                vplus
                wrote on last edited by
                #7

                I have developed following escape function that work fine with gridview rowfilter: public static string Escape(string aString) { string searchStr = Regex.Replace(aString, "[.']", "''"); searchStr = Regex.Replace(searchStr, "[.]]", "[]]"); searchStr = Regex.Replace(searchStr, "[.[]", "[[]"); searchStr = Regex.Replace(searchStr, "[.*]", "[*]"); searchStr = Regex.Replace(searchStr, "[.%]", "[%]"); return searchStr; } Before passing string to rowfilter just escape your search string with above function. Should work:thumbsup:

                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