Link to home
Start Free TrialLog in
Avatar of Epitel0920
Epitel0920

asked on

How do I execute several lines of SQL commands using C#?

Hello,
  I would like to execute several lines of SQL commands through the C# program. I have the complete text of the commands stored in a string s. However I can not just use:
SqlCommand command = connection.CreateCommand();
command.CommandText = s;
command.ExecuteNonQuery();
since the command text contains several "GO" statements and these statements have to be executed in order. Hence one statement have to finish executing before the other one starts.
One way is to break up s into several statements using "GO" as a delimeter... however that seems painful, especially since "GO" can be followed by "\n" for ex, or "GO" can be part of the variable name.
Is there any better build in way to do this?
Thank you
Peter
ASKER CERTIFIED SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Wrap your statement in this:
EXEC ('')

As in:
string sql = "EXEC('SELECT * FROM xx WHERE yy GO\nUPDATE...')

Jim
I stand corrected.

Jim
Avatar of Epitel0920
Epitel0920

ASKER

Thank you.. I guess I will have to do it this way