逐字转义字符串文字
我有以下无法编译的字符串:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
要解决您的标题问题...
要转义逐字字符串文字中的引号,请使用 quote-escape-sequence
""
(这是两个引号字符)请参阅 MSDN 有关转义等的更多详细信息。
请注意,在您发布的代码中,唯一的逐字字符串是第一个一个(前面有
@
)。 后续字符串不是逐字的,因此正确的转义序列是\"
。您可以使用
string.Format
使其看起来更漂亮:To address your title question...
To escape the quote in a verbatim string literal, use the quote-escape-sequence
""
(that's two quote characters)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
:问题在于,并非所有要连接的字符串都是逐字字符串文字,只有连接的第一部分是。
换句话说,
是整个语句中构建最终字符串的唯一逐字文字。
您需要在其余字符串前面添加 @ 以使它们全部逐字显示。
这会让它看起来像:
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,
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:
在第一次引用结束之后,无论如何都不再使用 @ 符号,因此您可以自由使用转义字符。 尝试将“表”像 [table] 和 [field] 一样用“[”包裹起来,或者用 \ 转义“字符”。
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 \.
如果不能使用 SQL 参数,String.Format 可能比纯“+ 连接”更简洁和可读。
If you cannot use SQL Parameters, String.Format can be little cleaner and readable than pure "+ concatenation".
您想使用
\"
转义引号,而不是""
。像这样:
编辑:
进一步说明:
您只有一个
@
在您要连接的所有字符串中的第一个上。 在文字字符串(前面有@
)中,您可以使用双引号转义引号。在普通字符串中,它是斜杠引号。 。例如
You want to use
\"
to escape quotes, not""
.Like this:
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.
我也相信您在构建此查询之前正确转义了这些变量:)
I also trust that you are escaping these variables correctly before building this query :)
为什么你要引用列的字面名称,对我来说似乎没有必要。
"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.