【问题标题】:; expected c# transfer and carry big string VS2012;预期的c#传输和携带大字符串VS2012
【发布时间】:2013-11-04 18:23:51
【问题描述】:

我有

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

当我通过 presssiin 将它分成 2 列时,输入就像

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

出现错误

我该如何解决?

【问题讨论】:

  • errore appears 错误是...?

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


【解决方案1】:

尝试关注

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

【讨论】:

    【解决方案2】:

    在字符串前面使用@符号,这样就可以多行拆分字符串

    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 所指出的,将其设为逐字字符串 (with @) 将导致换行符成为字符串的一部分。所以你的字符串会是这样的:

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

    【讨论】:

    • 那么换行符就在字符串本身中;这可能是也可能不是有意的。
    • @Servy 作为 T-SQL 语句,我认为换行符/空格不会有任何副作用。
    • @Servy,字符串好像是SQL语句,空行肯定不会有效果的。
    • @TrevorElliott 也许,但作者仍然应该知道正在发生的事情;无论如何,这个问题不需要局限于特定的 SQL 案例。
    【解决方案3】:

    像这样。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-14
      • 1970-01-01
      • 2019-04-07
      • 1970-01-01
      • 2023-03-16
      • 1970-01-01
      • 1970-01-01
      • 2020-07-09
      相关资源
      最近更新 更多