Avoiding stenographic lines

I have the following line that will not compile:

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

Violating sections:

 ""table"" = 

and

 ""field"" = 

The compiler all moves in an escape sequence. Can anyone understand what is wrong?

+7
string c # escaping verbatim-string
source share
7 answers

The problem is that not all the strings you concatenate are string literals of the string, only the first part of the concatenation.

In other words,

 @"SELECT value1, '" 

is the only literal literal in the entire instruction for constructing the final line.

You need to add @ in front of the rest of your lines to make them all verbatim.

What would it look like this:

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

To ask a question about your name ...

To avoid quoting in a string literal, use the escape sequence quote "" (the two quotation marks)

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

For more information on shielding, see MSDN .

Note that in your published code, the only shorthand line is the very first (from @ to it). Subsequent lines are not verbatim, so the correct escape sequence will be \" .

You can make it more beautiful with string.Format :

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

You want to use \" to return quotes, not "" .

Like this:

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

Edit:

Further explanation:

You only have @ first of all the strings you are concatenating. In literal lines (with @ in front), you avoid double-quoted quotes. In regular lines, this is a slash.

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."; 
+5
source share

Well, after your first end of the quote, the @ character is no longer used, so you can use the escape character. Try putting your "table" wrapped in "[", like [table] and [field] or escaping a character with the \ character.

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

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

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

I also believe that you are saving these variables correctly before building this query :)

+1
source share

Why you quote literal column names seems to me unnecessary.

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

Not tested, but I think you get this idea.

+1
source share

All Articles