我可能没有问谷歌的正确问题来找到答案。我只想保持我的代码整洁,而不是在一行上有一个很长的字符串。我想在不破坏字符串的情况下移动到下一行。
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)";
例如,我想将其分成两行而不影响字符串。所有的帮助将不胜感激。
答案 0 :(得分:6)
我怀疑你想要的是将@
用于逐字字符串文字;逐字字符串的优点是不处理转义序列,并且它们可以跨越多行。
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)";
答案 1 :(得分: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)";
答案 2 :(得分:3)
您可以在字符串前使用@
。这称为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)";
答案 3 :(得分: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)";
答案 4 :(得分: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)";
答案 5 :(得分: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)";
答案 6 :(得分:0)
喜欢这个吗?
cmd.CommandText = "text text" +
"text text";