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. Encoding user input

Encoding user input

Scheduled Pinned Locked Moved Database
csharpdatabasequestion
3 Posts 3 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.
  • A Offline
    A Offline
    Adam Durity
    wrote on last edited by
    #1

    Hey, Are there any predefined functions in .NET to encode user input for SQL statement compatablity (ie. change "'" to "\'", etc)? So: "Sam's cat died." would become "Sam\'s cat died." Thanks, Adam -- Adam "If you can't beat your computer in chess, try kickboxing"

    A R 2 Replies Last reply
    0
    • A Adam Durity

      Hey, Are there any predefined functions in .NET to encode user input for SQL statement compatablity (ie. change "'" to "\'", etc)? So: "Sam's cat died." would become "Sam\'s cat died." Thanks, Adam -- Adam "If you can't beat your computer in chess, try kickboxing"

      A Offline
      A Offline
      Alexander Kojevnikov
      wrote on last edited by
      #2

      You should avoid encoding user input into your SQL queries. This is a major security hole: SELECT MyColumn FROM MyTable WHERE MyOtherColumn={1} can become SELECT MyColumn FROM MyTable WHERE MyOtherColumn=0; DELETE FROM MyTable A better solution is to use stored procedures and pass user input as their parameters. Alexandre Kojevnikov MCAD charter member Leuven, Belgium

      1 Reply Last reply
      0
      • A Adam Durity

        Hey, Are there any predefined functions in .NET to encode user input for SQL statement compatablity (ie. change "'" to "\'", etc)? So: "Sam's cat died." would become "Sam\'s cat died." Thanks, Adam -- Adam "If you can't beat your computer in chess, try kickboxing"

        R Offline
        R Offline
        Rocky Moore
        wrote on last edited by
        #3

        If you are using SqlCommand's or DataAdpaters, you can use parameters for your values and it will automatically encode them for you. When you specify you DataAdpater query you can put: SELECT au_id, au_lname, au_fname from authors where au_fname = @au_fname Then in your command: myCmd.Parameters["@au_fname"].value = "'any'''input"; And all should be well. Another approach is to simply to a string replace on your input replacing all single quotes with two single quotes. Rocky Moore <><

        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