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.
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)
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 + @"';";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With