6

Possible Duplicate:
Multiline String Literal in C#

I'm probably not asking the right questions of Google to find my answer. I just want to keep my code neat without having a really long string on one line. I want to move to the next line without breaking the string.

cmd.CommandText = "UPDATE players SET firstname = CASE id WHEN 1 THEN 'Jamie' WHEN 2 THEN 'Steve' WHEN 3 THEN 'Paula' END WHERE id IN (1,2,3)";

For example, I would like to break this into two lines without affecting the string. All help would be appreciated.

Community
  • 1
  • 1
webby68
  • 420
  • 3
  • 10
  • 27

7 Answers7

6

I suspect what you want is to use the @ for verbatim string literals; the advantage of verbatim strings is that escape sequences are not processed, and that they can span multiple lines.

cmd.CommandText = @"UPDATE players 
                    SET firstname = 
                      CASE id 
                        WHEN 1 THEN 'Jamie' 
                        WHEN 2 THEN 'Steve' 
                        WHEN 3 THEN 'Paula' 
                      END 
                    WHERE id IN (1,2,3)";
Joachim Isaksson
  • 176,943
  • 25
  • 281
  • 294
4

Use @ symbol before string. It will say to compiler that string is multiline.

cmd.CommandText = @"UPDATE players 
                    SET firstname = CASE id 
                        WHEN 1 THEN 'Jamie' 
                        WHEN 2 THEN 'Steve' 
                        WHEN 3 THEN 'Paula' 
                        END WHERE id IN (1,2,3)";
Dmytro
  • 16,668
  • 27
  • 80
  • 130
3

You could use @ in fronf of your string. This is called Verbatim String Literal

cmd.CommandText = @"
 UPDATE players 
 SET firstname = CASE id 
                 WHEN 1 THEN 'Jamie' 
                 WHEN 2 THEN 'Steve' 
                 WHEN 3 THEN 'Paula' 
                 END 
 WHERE id IN (1,2,3)";
Claudio Redi
  • 67,454
  • 15
  • 130
  • 155
1

like this

cmd.CommandText = "UPDATE players SET firstname =" +
   " CASE id WHEN 1 THEN 'Jamie' WHEN 2 THEN 'Steve' WHEN 3" + 
   " THEN 'Paula' END WHERE id IN (1,2,3)";
Scott Selby
  • 9,420
  • 12
  • 57
  • 96
  • @ works as well, keep in mind with the way I showed , you have to remember to space at the beginning of the next line as prevent concatinating 2 words that aren't supposed to be – Scott Selby Sep 28 '12 at 17:53
  • @webby68 - its good to remember this as an option , even though it doesn't work quite as cleanly as the @ approach - if you ever have to write the same thing in VB.Net , the @ before a string is not allowed – Scott Selby Sep 28 '12 at 18:11
0

Simpley concatenate your strings as follows:

cmd.CommandText = "UPDATE players SET firstname = CASE id WHEN 1 " 
cmd.CommandText +="THEN 'Jamie' WHEN 2 THEN 'Steve' WHEN 3 THEN 'Paula' END WHERE id IN (1,2,3)";
Joel Etherton
  • 37,325
  • 10
  • 89
  • 104
JSK NS
  • 3,346
  • 2
  • 25
  • 42
0

It's called concatenation:

cmd.CommandText = "UPDATE players SET firstname" +
    " = CASE id WHEN 1 THEN 'Jamie' WHEN 2 THEN 'Steve'" +
    " WHEN 3 THEN 'Paula' END WHERE id IN (1,2,3)";
Joel Etherton
  • 37,325
  • 10
  • 89
  • 104
0

Like this?

cmd.CommandText = "text text" +
  "text text";
Tim Destan
  • 2,028
  • 12
  • 16