Double double quotes in the string

半腔热情 提交于 2019-12-11 11:40:27

问题


In c#: I need to create oracle query string like this:

string orQr = @"
    SELECT ""Date"", ""Key""
    FROM TBL
";

I need to do it dynamicly. But there is a problem with escaping double-double quotes.

How to do that? This is a little mad ;-) and doesn't work:

string quotes = @"""""";
string subSlct = quotes + "Date" + quotes + ", " + quotes + "Key" + quotes;
string orQrB = @"
    SELECT " + subSlct + @"
    FROM TBL
";

(the result is: SELECT \"\"Date\"\", \"\"Key\"\"\ FROM TBL )


回答1:


Your quotes variable is adding two double quotes, instead of one. Change this:

string quotes = @"""""";

to this:

string quotes = @"""";

Also:

  1. There's nothing wrong with string.Format("SELECT \"{0}\", \"{1}\" FROM TBL", a, b);
  2. Your code is susceptible to SQL injection.

    In general, you'd want to use parameterized queries, but since these don't allow parameterized column names, you'll want to at the very least sanitize the input yourself and check for illegal characters (e.g., ;, --).




回答2:


Does this work:

string quotes = "\"\"";



回答3:


Don't construct your query by string concatenation. You open yourself to SQL injection attacks. Use parameterized queries and you will also be able to include the quotes more easily.




回答4:


You can just use normal escaping like: string quotes = "\"\"";



来源:https://stackoverflow.com/questions/25015520/double-double-quotes-in-the-string

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!