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
  • 422
  • 3
  • 10
  • 26

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
  • 170,943
  • 22
  • 265
  • 283
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
  • 15,728
  • 24
  • 75
  • 125
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
  • 65,896
  • 14
  • 126
  • 152
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,280
  • 12
  • 52
  • 93
  • @ 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
  • 36,697
  • 10
  • 85
  • 101
JSK NS
  • 3,226
  • 2
  • 23
  • 40
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
  • 36,697
  • 10
  • 85
  • 101
0

Like this?

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