Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid double quote escape in C# strings?

In Ruby it's possible to use the strings without the need to escape the double quote in the string, like Q/Some my string with "double quotes"/

Is it possible with C# to use a string without a need to escape double quotes? For a good reason I need to write inline SQL and it's very annoying to escape double quotes every time I put SQL query from DB console to C# code.

I know it's possible to use \" or "" as one double quote. But is it possible to avoid the need to escape the double quotes?

like image 592
Zelid Avatar asked Aug 03 '12 08:08

Zelid


2 Answers

No, basically. You have the choice of "foo \" bar" or @"foo "" bar", both of which you have mentioned. However, frankly I rarely find " necessary in SQL; you have ' for literal strings and [ / ] for object/column names - of course, this might just be because I usually use SQL Server.

The only alternative that doesn't involve any escaping is to move the SQL to an external resource file, maybe a text file. That, though, is probably more painful than just using "".

like image 160
Marc Gravell Avatar answered Oct 24 '22 01:10

Marc Gravell


No, this is not possible in C#. You could write your SQL in a separate file and then read it from there.

like image 34
Darin Dimitrov Avatar answered Oct 24 '22 01:10

Darin Dimitrov