6

可能重复:
C# 中的多行字符串文字

我可能没有向 Google 提出正确的问题来找到我的答案。我只想保持我的代码整洁,而不是在一行上有一个很长的字符串。我想在不破坏字符串的情况下移至下一行。

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

例如,我想把它分成两行而不影响字符串。所有帮助将不胜感激。

4

7 回答 7

6

我怀疑您想要的是使用@for 逐字字符串文字;逐字字符串的优点是不处理转义序列,并且它们可以跨越多行。

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)";
于 2012-09-28T17:50:43.297 回答
4

@在字符串前使用符号。它会告诉编译器字符串是多行的。

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)";
于 2012-09-28T17:51:04.647 回答
3

你可以@在你的字符串前面使用。这称为逐字字符串文字

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)";
于 2012-09-28T17:50:19.030 回答
1

像这样

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)";
于 2012-09-28T17:50:13.663 回答
0

Simpley 连接您的字符串,如下所示:

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)";
于 2012-09-28T17:50:15.567 回答
0

它被称为串联:

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)";
于 2012-09-28T17:50:15.780 回答
0

像这样?

cmd.CommandText = "text text" +
  "text text";
于 2012-09-28T17:50:18.133 回答