Solved

Handle " character in query statement string in C#

Posted on 2011-02-15
3
455 Views
Last Modified: 2012-05-11
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.
0
Comment
Question by:minglelinch
  • 2
3 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 34898788
You need to "escape" your quotation mark within the query:
string sqlstring = "  select name from mytable where name is not null and name not like '\"%'  ";

Open in new window

0
 
LVL 1

Author Comment

by:minglelinch
ID: 34898863
Thanks a lot. I had put the back slash in a wrong place before.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 34899089
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

0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
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.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

713 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