/// <summary> /// Executes the scripts /// </summary> /// <returns>false if usuccessful</returns> public bool Execute() { if (this.outputCompoundFile) { FileBuilder.SetPath(this.compoundFile); FileBuilder.Initialize(); } this.ValidateConfiguration(); this.server.ConnectionContext.InfoMessage += new System.Data.SqlClient.SqlInfoMessageEventHandler(this.ConnectionContext_InfoMessage); lock (this.syncRoot) { this.stopping = false; var sortedList = from item in this.config.ExecutionSequence orderby item.Key select item; if (this.stopping) { this.RaiseStoppedEvent(); return(false); } if (this.PrependUsingDatabaseName && this.outputCompoundFile) { FileBuilder.AddText(string.Format("use [{0}]", this.database.Name)); FileBuilder.AddText("GO"); } foreach (KeyValuePair <int, string> kvp in sortedList) { if (this.stopping) { this.RaiseStoppedEvent(); return(false); } this.OnProgress(new ScriptExecutorEventArgs[] { new ScriptExecutorEventArgs(ScriptExecutorEventArgs.EventMessageCode.ScriptExecution, kvp.Value) }); this.ExecuteScriptFile(kvp.Value); } this.OnProgress(new ScriptExecutorEventArgs[] { new ScriptExecutorEventArgs(ScriptExecutorEventArgs.EventMessageCode.ExecutionComplete, string.Empty) }); if (this.outputCompoundFile) { FileBuilder.Flush(); } return(true); } }
/// <summary> /// Executes the script file. /// </summary> /// <param name="filename">The filename.</param> private void ExecuteScriptFile(string filename) { string sqlCommand = string.Empty; using (StreamReader streamReader = new StreamReader(filename)) { sqlCommand = streamReader.ReadToEnd(); streamReader.Close(); } this.database.ExecuteNonQuery(sqlCommand.Replace(DatabaseReplacementToken, this.database.Name), ExecutionTypes.Default); if (this.outputCompoundFile) { FileBuilder.AddText(sqlCommand.Replace(DatabaseReplacementToken, this.database.Name)); } }