Solved

C# SQL Server Insert Embedded quotes

Posted on 2014-09-26
3
187 Views
Last Modified: 2014-10-08
I want to do an insert into an SQL Server table but some of the variables have embeded quotes and commas

How do I insert variables that have embedded quotes and commas?

Example

strContactName = Drake, Tree, Castles  (has a comma)
strContactLocation = Tree's are Us   (has a single quote)

When I try and Insert these I get an error how do I work around it.

sql = "insert into Contacts(ContactName, ContactLocation) ";
sql = sql + " values('" + strContactName + "'," + strContactLocation + "')";
cmdw.CommandText = sql;
0
Comment
Question by:Idarac
3 Comments
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 500 total points
ID: 40347170
The simplest way to do this is to double up single quotes in string values (commas shouldn't be a problem, note the syntax error in your posted code, a missing quote after the comma in the value list).
sql = sql + " values('" + strContactName.Replace("'", "''") + "', '" + strContactLocation.Replace("'", "''") + "')";

Open in new window

However you may want to look into using Command parameters as that's the better way to do this:
            sql = "insert into Contacts(ContactName, ContactLocation) ";
            sql = sql + " values(@ContactName, @ContactLocation)";
            cmdw.CommandText = sql;
            cmdw.Parameters.Add(new SqlParameter("@ContactName", strContactName));
            cmdw.Parameters.Add(new SqlParameter("@ContactLocation", strContactLocation));
            cmdw.ExecuteNonQuery();

Open in new window

Now you don't have to mess with the value of your variables. Also, if you ever have to insert multiple records in the same table you don't have to change the CommandText each time, just set new values for the parameter objects and it will be faster and more efficient.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40347579
I second the use of parameterized queries. Using string concatenation to build queries opens you up to SQL Injection attacks.
0
 
LVL 1

Author Closing Comment

by:Idarac
ID: 40369753
Thank you
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Server Error 11 58
ASP.NET 5 Templates 2 78
System.Security 2 27
localhost: This site can’t be reached  localhost refused to connect. 2 56
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question