逐字转义字符串文字

发布于 2024-07-15 02:28:21 字数 357 浏览 10 评论 0原文

我有以下无法编译的字符串:

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

有问题的部分是 :

""table"" =

""field"" = 

编译器在转义序列上混淆了。 谁能看出出了什么问题吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

顾冷 2024-07-22 02:28:21

要解决您的标题问题...

要转义逐字字符串文字中的引号,请使用 quote-escape-sequence "" (这是两个引号字符)

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

请参阅 MSDN 有关转义等的更多详细信息。

请注意,在您发布的代码中,唯一的逐字字符串是第一个一个(前面有 @)。 后续字符串不是逐字的,因此正确的转义序列是 \"

您可以使用 string.Format 使其看起来更漂亮:

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

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.

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 formLookupPull = 
   string.Format(@"SELECT value1, '{0}', '{1}' FROM lkpLookups" +
                 @"WHERE ""table"" = '{0}' and ""field"" = '{1}';", 
                 tableName, columnName)
小嗷兮 2024-07-22 02:28:21

问题在于,并非所有要连接的字符串都是逐字字符串文字,只有连接的第一部分是。

换句话说,

@"SELECT value1, '"

是整个语句中构建最终字符串的唯一逐字文字。

您需要在其余字符串前面添加 @ 以使它们全部逐字显示。

这会让它看起来像:

String formLookupPull = @"SELECT value1, '"+tableName+ @"', '"+columnName+ @"' FROM lkpLookups WHERE ""table"" = '" + tableName + @"' and ""field"" = '" + 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.

Which would make it look like:

String formLookupPull = @"SELECT value1, '"+tableName+ @"', '"+columnName+ @"' FROM lkpLookups WHERE ""table"" = '" + tableName + @"' and ""field"" = '" + columnName + @"';";
淡忘如思 2024-07-22 02:28:21

在第一次引用结束之后,无论如何都不再使用 @ 符号,因此您可以自由使用转义字符。 尝试将“表”像 [table] 和 [field] 一样用“[”包裹起来,或者用 \ 转义“字符”。

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

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 \.

String formLookupPull = @"SELECT value1, '" + tableName + "', '" + columnName + "' FROM lkpLookups WHERE [table] = '" + tableName + "' and [field] = '" + columnName + "';";
满身野味 2024-07-22 02:28:21

如果不能使用 SQL 参数,String.Format 可能比纯“+ 连接”更简洁和可读。

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

If you cannot use SQL Parameters, String.Format can be little cleaner and readable than pure "+ concatenation".

string formLookupPull = 
  string.Format(@"SELECT value1, '{0}', '{1}' 
                       FROM lkpLookups 
                   WHERE ""table"" = '{0}' AND ""field"" = '{1}';",
                tableName, columnName);
风启觞 2024-07-22 02:28:21

您想使用 \" 转义引号,而不是 ""

像这样:

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

编辑:

进一步说明:

您只有一个 @ 在您要连接的所有字符串中的第一个上。 在文字字符串(前面有 @)中,您可以使用双引号转义引号。在普通字符串中,它是斜杠引号。 。

例如

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.";

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.";
走野 2024-07-22 02:28:21
String formLookupPull = @"SELECT value1, '"+tableName+"', '"+columnName+"' FROM lkpLookups WHERE \"table\" = '" + tableName + "' and \"field\" = '" + 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 :)

潜移默化 2024-07-22 02:28:21

为什么你要引用列的字面名称,对我来说似乎没有必要。

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

没有经过测试,但我想你会明白的。

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 + "';";

Not tested but I think you will get the idea.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文