GO statements blowing up sql execution in .NET

前端 未结 11 1035
南笙
南笙 2020-12-05 09:48

I have a very simple C# command shell app that executes a sql script generated by SQL Server for scripting schema and data. It\'s blowing up on the \"GO\" statements. Error

11条回答
  •  天命终不由人
    2020-12-05 10:17

    The top answer has a mistake. I just tested a working solution: You should allow space,';' or new line before GO

                var scripts = Regex.Split(statementText, @"(\s+|;|\n|\r)GO", RegexOptions.Multiline);
                foreach(var splitScript in scripts.Where(splitScript => !splitScript.IsNullOrWhiteSpace())) {
                    cmd.CommandText = splitScript;
                    cmd.ExecuteNonQuery();
                }
    

提交回复
热议问题