NekoManu Posted June 25, 2008 Posted June 25, 2008 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 Quote
Administrators PlausiblyDamp Posted June 25, 2008 Administrators Posted June 25, 2008 You really should be using parameterised sql rather than string concatenation anyway... http://www.xtremedotnettalk.com/showthread.php?p=463520#post463520 Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
stumper66 Posted July 4, 2008 Posted July 4, 2008 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] Quote
Administrators PlausiblyDamp Posted July 4, 2008 Administrators Posted July 4, 2008 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. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.