Prev: Using Dispatcher.BeginInvoke with current foreach member
Next: Enterprise design: Accessing global 'config' data
From: shapper on 19 Nov 2009 08:56 On Nov 18, 9:19 am, JTC <J...(a)discussions.microsoft.com> wrote: > I assume you mean you have a file containing the SQL. If this is the case, > read the content of the file as a string, replace to GO statements with a > ";". Execute the string. > > However, not all sql statements can be executed in a batch, so you may want > to split the string to create an array. Loop through the array then executing > each statement. > > something like... > > string text = System.IO.File.ReadAllText("myscript.sql"); > text = text.Replace("GO", ";"); > string[] statements = text.Split(Char.Parse(";")); > > for(int i = 0; i < statements.Length ;i++) > { > System.Data.SqlClient.SqlCommand cmd = new > System.Data.SqlClient.SqlCommand(statements[i], conn) > conn.Open(); > > cmd.ExecuteNonQuery(); > > } > > NOTE: "GO" and ";" in comments will present a problem. I think that if you use the approach I posted before you don't need to split the string or worry about the GO's. At least it works fine for me and my script creates a Database, Filegroups, Logs, Tables, Constraints, etc. Thanks, Miguel |