Celebrating 8 Successful Years
+1 (315)-715-8489

Executing Sql Scripts with Go Statements as CommandText

Here is the code lines for executing the sql script . we may get the errors while executing any string as CommandText that loaded with sql script. This causes due to the GO Statements. I'm providing these code lines to split that Go statements. Hope this is helpful to execute script file with Go Statements.


                 string sqlBatch = string.Empty;
                    script += "\nGO";
                    SqlCommand cmd = new SqlCommand("", conn);
                    foreach (string line in script.Split(new string[2] { "\n", "\r" }, StringSplitOptions.RemoveEmptyEntries))
                    {
                        if (line.ToUpperInvariant().Trim() == "GO")
                        {
 
                            cmd.CommandText = sqlBatch;
                            cmd.ExecuteNonQuery();
                            sqlBatch = string.Empty;
                        }
                        else
                        {
                            sqlBatch += line + "\n";
                        }
                    }​


Sai Venkat - Software Developer

Loading

Get In Touch

Presentations

© Copyright 2016 by SKG Infosolution. All rights reserved

Top