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.

1
  • @ColeJohnson \n won't work on source file. Commented Sep 28, 2012 at 17:49

7 Answers 7

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)";
Sign up to request clarification or add additional context in comments.

1 Comment

This was perfect. I'm sure the others work equally as well. Thanks to all who helped.
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)";

Comments

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)";

Comments

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)";

2 Comments

@ 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
@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
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)";

Comments

0

Like this?

cmd.CommandText = "text text" +
  "text text";

Comments

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)";

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.