Click here to Skip to main content
15,566,615 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

How can I concatenate a double quoted string? I should get a result like shown below. But how I have to concatenate "Provider Review" and need need to keep "" in this query, but my logic is not working.

This should be the final result: sQury2 =  select Distinct  , name, Decis.code  "Provider Review"

What I have tried:

C#
 string sQury2 = " select Distinct" + "\r\n";

 sQury2 = sQury2 + "   , name" + "\r\n";

 sQury2 = sQury2 + " , Decis.code  "Provider Review" + "\r\n"; // how to change this qry?
Posted
Updated 1-Feb-22 6:26am
v3

Quote:
How to concatenate string having double quotes in C#

Escape the double quote in the string.
"bla \"Provider Review\" bla"

Escaping in C#: characters, strings, string formats, keywords, identifiers[^]
 
Share this answer
 
The problem is that that's an SQL command - so double quotes aren't applicable anyway, unless you want them as part of an actual string.
SQL uses single quotes for string delimiters, not double quotes, so to return a string literal all you would do is surround it with double quotes:
SQL
SELECT 'Hello world!';

If you are trying to "build a query" and return a column called Provider Review then you need to use either square brackets (SQL Server) or backquotes (Access):
SQL
SELECT [Provider Review] FROM MYTable


But the Query you show doesn;t make it at all obvious what exactly you want - it isn't valid SQL at all because Distinct is a keyword which needs to be followed by a column name rather than a comma.

If you need to put double quotes in a C# string literal, there are two ways
1) Escape it:
C#
string s = "this is surrounded by \"double quotes\"";

2) Escape the string and double them:
C#
string s = @"this is surrounded by ""double quotes""";
 
Share this answer
 
Comments
CHill60 1-Feb-22 11:34am    
FYI there are versions of MS SQL that will allow double quotes instead of the [ ] surrounding a column name ... e.g.
create table temp ("silly column name" varchar(30));
insert into temp ([silly column name]) values ('how horrible');
SELECT "silly column name" from temp;
You can user the query like to get in single line:
string sQury2 = " select Distinct";
sQury2 = sQury2 + " , name";
sQury2 = sQury2 + " , Decis.code \"Provider Review\"";

Result: sQury2 = select Distinct , name , Decis.code "Provider Review"
Use the following to break into lines like:
string sQury2 = " select Distinct" + "\r\n";
sQury2 = sQury2 + "   , name" + "\r\n";
sQury2 = sQury2 + " , Decis.code  \"Provider Review\"" + "\r\n";
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900