-2

I have

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

Whene I divide it in 2 columns by presssiin enter like

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

How can I solve that?

Tilak
  • 30,108
  • 19
  • 83
  • 131
streamc
  • 676
  • 3
  • 11
  • 27

3 Answers3

5

Try following

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

or

CmdString = @"insert into Team_table (name1, name2, result1, result2) 
(select t1.name,t2.name,NULL,NULL from teams t2 cross join teams t1)";
Tilak
  • 30,108
  • 19
  • 83
  • 131
4

Use @ symbol in front of the string , then you can break the string on multiple lines

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

The reason you are getting the error is because the compiler is considering new line as a new C# statement, and it is expecting you to close the previous string and terminate the statement using ;

EDIT: As @Servy has pointed out, making it a verbatim string (with @) would result in the new line character to be part of string. So your string would be something like:

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

If you don't want the line break and want to split string on multiple lines, then you have to define a single string on each line and use concatenation like:

string CmdString = 
    "insert into Team_table (name1, name2, result1, result2) " + 
    "(select t1.name,t2.name,NULL,NULL from teams t2 cross join teams t1)";
Community
  • 1
  • 1
Habib
  • 219,104
  • 29
  • 407
  • 436
  • 4
    Then the newline is in the string itself; that may or may not be intended. – Servy Nov 04 '13 at 18:25
  • 1
    @Servy Being a T-SQL statement I don't think newlines/whitespace will have any side effects. – Trevor Elliott Nov 04 '13 at 18:27
  • 1
    @Servy, the string seems like a SQL statement, I am sure an empty line will not have an effect. – Habib Nov 04 '13 at 18:27
  • @TrevorElliott Perhaps, but the author should still be aware that of what's happening; the question need not be localized to the specific case of SQL anyway. – Servy Nov 04 '13 at 18:29
3

Like this.

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