;预计c#转移并携带大字符串VS2012

时间:2013-11-04 18:23:51

标签: c# sql visual-studio-2012 string-concatenation sqlcommand

我有

CmdString = "insert into Team_table (name1, name2, result1, result2) (select t1.name,t2.name,NULL,NULL from teams t2 cross join teams t1)";

当按下输入时将其分为2列,如

CmdString = "insert into Team_table (name1, name2, result1, result2) 
(select t1.name,t2.name,NULL,NULL from teams t2 cross join teams t1)";

错误出现

我该如何解决?

3 个答案:

答案 0 :(得分:5)

尝试以下

CmdString = "insert into Team_table (name1, name2, result1, result2)" + 
" (select t1.name,t2.name,NULL,NULL from teams t2 cross join teams t1)";

CmdString = @"insert into Team_table (name1, name2, result1, result2) 
(select t1.name,t2.name,NULL,NULL from teams t2 cross join teams t1)";

答案 1 :(得分:4)

在字符串前面使用@符号,然后可以在多行上打破字符串

string CmdString = @"insert into Team_table (name1, name2, result1, result2) 
                (select t1.name,t2.name,NULL,NULL from teams t2 cross join teams t1)";

您收到错误的原因是因为编译器将新行视为新的C#语句,并且它期望您关闭前一个字符串并使用;

终止语句 编辑:正如@Servy指出的那样,将其作为逐字字符串(带@)会导致新行字符成为字符串的一部分。所以你的字符串将是这样的:

"insert into Team_table (name1, name2, result1, result2) \r\n (select t1.name,t2.name,NULL,NULL from teams t2 cross join teams t1)"

如果你不想换行并希望在多行上拆分字符串,那么你必须在每一行上定义一个字符串并使用如下的连接:

string CmdString = 
    "insert into Team_table (name1, name2, result1, result2) " + 
    "(select t1.name,t2.name,NULL,NULL from teams t2 cross join teams t1)";

答案 2 :(得分:3)

喜欢这个。

    CmdString = "insert into Team_table (name1, name2, result1, result2)" + 
        "(select t1.name,t2.name,NULL,NULL from teams t2 cross join teams t1)";