Solved

Handle " character in query statement string in C#

Posted on 2011-02-15
3
456 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

Suggested Solutions

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
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…

738 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