I think you have two problems:
First, you call the ExecuteNonQuery method, which takes a string, not a StringCollection. I believe that this method does not recognize GO used to separate batch statements. Instead, the documentation of the ExecuteNonQuery method that accepts a StringCollection indicates that GOs are recognized
The second problem is the passed ExecutionTypes.ContinueOnError . In this case, the same documentation states that you cannot receive exceptions if something fails.
I think the best approach should be to split the input text by GO and then build a StringCollection to go to the ExecuteNonQuery method and check the returned array of the affected strings. Something like this (should be checked)
using (SqlConnection sqlConnection = new SqlConnection(connectionString)) { ServerConnection svrConnection = new ServerConnection(sqlConnection); Server server = new Server(svrConnection); string script = File.ReadAllText("upgradeDatabase.sql"); string[] singleCommand = Regex.Split(script, "^GO", RegexOptions.Multiline); StringCollection scl = new StringCollection(); foreach(string t in singleCommand) { if(t.Trim().Length > 0) scl.Add(t.Trim()); } try { int[] result = server.ConnectionContext.ExecuteNonQuery(scl, ExecutionTypes.ContinueOnError);
Of course, the alternative executes each individual statement and removes the ContinueOnError flag. Then write down and write down the possible exceptions. But it will, of course, be slower.
source share