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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I stand corrected.
Jim
Jim
ASKER
Thank you.. I guess I will have to do it this way
EXEC ('')
As in:
string sql = "EXEC('SELECT * FROM xx WHERE yy GO\nUPDATE...')
Jim