简体   繁体   English

Escaping 逐字字符串文字

[英]Escaping verbatim string literals

I have the following string which won't compile:我有以下无法编译的字符串:

String formLookupPull = @"SELECT value1, '"+tableName+"', '"+columnName+"' FROM lkpLookups WHERE ""table"" = '" + tableName + "' and ""field"" = '" + columnName + "';";

The offending sections are:违规的部分是:

""table"" =

and

""field"" = 

The compiler is getting all mixed up on the escape sequence.编译器在转义序列上搞混了。 Can anyone see what's wrong?任何人都可以看到有什么问题吗?

To address your title question...为了解决您的标题问题...

To escape the quote in a verbatim string literal, use the quote-escape-sequence "" (that's two quote characters)要在逐字字符串文字中转义引号,请使用引号转义序列"" (即两个引号字符)

string a = @"He said ""Hi!""..."; // He said "Hi!"...

See MSDN for more details on escaping, etc.有关 escaping 等的更多详细信息,请参阅MSDN

Note that in your posted code, the only verbatim string is the very first one (with the @ before it).请注意,在您发布的代码中,唯一的逐字字符串是第一个字符串(前面有@ )。 The subsequent strings are not verbatim, so the proper escape sequence would be \" .随后的字符串不是逐字记录的,因此正确的转义序列应该是\"

You can make it look prettier with string.Format :您可以使用string.Format使它看起来更漂亮:

String formLookupPull = 
   string.Format(@"SELECT value1, '{0}', '{1}' FROM lkpLookups" +
                 @"WHERE ""table"" = '{0}' and ""field"" = '{1}';", 
                 tableName, columnName)

The problem is that not all the strings you are concatenating are verbatim string literals, only the first portion of the concatenation is.问题在于,并非您要连接的所有字符串都是逐字字符串文字,只有连接的第一部分是。

In other words,换句话说,

@"SELECT value1, '"

is the only verbatim literal in the entire statement to build the final string.是整个语句中用于构建最终字符串的唯一逐字文字。

You would need to add @ in front of the rest of your strings to make them all verbatim.您需要在字符串的 rest 前面添加 @ 以使它们全部逐字记录。

Which would make it look like:这将使它看起来像:

String formLookupPull = @"SELECT value1, '"+tableName+ @"', '"+columnName+ @"' FROM lkpLookups WHERE ""table"" = '" + tableName + @"' and ""field"" = '" + columnName + @"';";

You want to use \" to escape quotes, not "" .您想使用\"来转义引号,而不是""

Like this:像这样:

.. FROM lkpLookups WHERE \"table\" = '" ..

Edit:编辑:

Further explanation:进一步说明:

You only have an @ on the first of all the strings you're concatenating.您连接的所有字符串的第一个只有一个@ In literal strings (with an @ in front) you escape quotes with a double quote.在文字字符串(前面有一个@ )中,你用双引号转义引号。 In normal strings, it's slash-quote.在普通字符串中,它是斜线引号。

Eg.例如。

string s = @"this is a literal string with ""quotes"" in it, " 
         +  "and this is a normal string with \"quotes\" in it";

string t = @"two literal strings" + @", concatenated together.";

Well after your first end of quote, the @ symbol is no longer being used anyways so you are free to use the escape character.好吧,在您第一次引用结束之后,无论如何都不再使用 @ 符号,因此您可以自由使用转义字符。 Try putting your "table" wrapped in '[' like [table] and [field] or escaping the " character with a \.尝试将您的“表”包裹在“[”中,例如 [table] 和 [field] 或 escaping 将“字符与 \.

String formLookupPull = @"SELECT value1, '" + tableName + "', '" + columnName + "' FROM lkpLookups WHERE [table] = '" + tableName + "' and [field] = '" + columnName + "';";

If you cannot use SQL Parameters, String.Format can be little cleaner and readable than pure "+ concatenation".如果您不能使用 SQL 参数,则 String.Format 可能比纯“+ 连接”更清晰易读。

string formLookupPull = 
  string.Format(@"SELECT value1, '{0}', '{1}' 
                       FROM lkpLookups 
                   WHERE ""table"" = '{0}' AND ""field"" = '{1}';",
                tableName, columnName);
String formLookupPull = @"SELECT value1, '"+tableName+"', '"+columnName+"' FROM lkpLookups WHERE \"table\" = '" + tableName + "' and \"field\" = '" + columnName + "';";

I also trust that you are escaping these variables correctly before building this query:)我也相信你是 escaping 这些变量在构建此查询之前正确:)

Why are you quoting the literal names of the columns, seem unnecessary to me.你为什么引用列的字面名称,对我来说似乎没有必要。

"SELECT value1, " + tableName + "," + columnName +" FROM lkpLookups WHERE table = '" + tableName + "' and field = '" = columnName + "';"; "SELECT value1, " + tableName + "," + columnName +" FROM lkpLookups WHERE table = '" + tableName + "' and field = '" = columnName + "';";

Not tested but I think you will get the idea.未经测试,但我想你会明白的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM