Link to home
Start Free TrialLog in
Avatar of minglelinch
minglelinch

asked on

Handle " character in query statement string in C#

I have a problem to compose a query statement in C# with the record starting with " character.
I want to select a record which does not contain character double quote ".
e.g. select name from mytable where name is not null and name not like '"%'

In C#, When I
string sqlstring = "  select name from mytable where name is not null and name not like '"%'  ";
it looks like '"%'  is not right. ???
Thanks.
ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of minglelinch
minglelinch

ASKER

Thanks a lot. I had put the back slash in a wrong place before.
NP. Glad to help.

Alternative to the above, you can turn your string into a "raw" string, where backslash is just a backslash, by prefixing the string with an @ symbol. You would then escape your double-quotes with double-quotes themselves (a-la VB). Backslashes would not escape anything--even \n or \r.
string sqlstring = @"  select name from mytable where name is not null and name not like '""%'  ";

Open in new window