Click to See Complete Forum and Search --> : [RESOLVED] Help with SQL in C#


Nebulus64
February 11th, 2008, 08:55 AM
Hello everyone,

I have a piece of code here that doesn't appear to work, and I think I know why, but I just can't get my head around a way to fix it. I'm am making a select statement from a database and doing a string comparison:

<code>
comm.CommandText = "SELECT Insurance_Company_Number, Insurance_Company_Name FROM TABLE_Insurance_Company WHERE Insurance_Company_Number = 'Hudson's Bay Co (Toronto)'";
</code>

Now, I'm certain the problem arises because of the apostrophe in Hudson's screwing around with the string, is there an escape sequence that I can use to use the ' as part of the string?

The error that comes up is:
Syntax Error (missing operator) in query expression 'Insurance_Company_Number = 'Hudson's Bay Co (Toronto)"

jshultz
February 11th, 2008, 09:12 AM
The code tags actually use [] not <>.

The escape in SQL is the apostrophe. So, in your string you would need to do two apostrophe's in order to escape it.

Nebulus64
February 11th, 2008, 09:19 AM
Thanks for the help, seemed to get the job done.