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
Solved

Handle " character in query statement string in C#

Posted on 2011-02-15
3
454 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
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…

808 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