Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I want to insert names into a database. Everything works fine until I want to insert a name with a quote.

 

My insert command is:

strSql = "Insert Into Individual (Name, FirstName) Values ('" & strINDIName & "', '" & strINDIFirstName & "')"

 

This works fine when I want to insert a name like: Cain

But it does NOT work with: O'Cain

  • 2 weeks later...
Posted

This is how I do it:

 

Old:

strSql = "Insert Into Individual (Name, FirstName) Values ('" & strINDIName & "', '" & strINDIFirstName & "')"[/Code]

 

Change to:

[Code]
strSql = "Insert Into Individual (Name, FirstName) Values ('" & strINDIName.Replace("'", "''") & "', '" & strINDIFirstName.Replace("'", "''") & "')"[/Code]

Or to clean it up:

[Code]
strSql = String.Format("Insert Into Individual (Name, FirstName) Values ('{0}', '{1}')", _
strINDIName.Replace("'", "''"), _
strINDIFirstName.Replace("'", "''"))[/Code]

  • Administrators
Posted

Parameters are still a lot safer and cleaner removing the single quotes won't prevent other forms of injection attack. You are also creating extra work somewhere else - either the SQL code now needs to convert all double ' to single ones to store things correctly or any code that retreives these records needs to remove the duplicate ' character. If the data isn't only used by your app then any other applications, web pages, reports etc. are now resposible for cleaning up the returned vales.

 

Parameters really are safer and cleaner as solutions go.

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...