Reputation: 4224
If I open SQL Management Studio and write the following text:
select a =1
GO
select b =2
SQL Management Studio splits the text into two statements, striping on the 'GO' keyword.
How should I do the same thing in C#? I could use a regex, or string splitting, but I'm not able to split correctly a string like this:
select text ='
this is a unique statement
GO
'
Upvotes: 0
Views: 1665
Reputation: 1228
Like you already know, GO is the default batch separator in SQL management studio, not a SQL keyword.
However management studio has library's available that you can use in C#. Explained here (option 2)
string connectionString, scriptText;
SqlConnection sqlConnection = new SqlConnection(connectionString);
ServerConnection svrConnection = new ServerConnection(sqlConnection);
Server server = new Server(svrConnection);
server.ConnectionContext.ExecuteNonQuery(scriptText);
This will execute "scriptText" as if you execute it in management studio. and it will handle "GO" as a batch separator by default.
Upvotes: 3