public void Commit(ChangeScript[] scripts) { _logger.LogMessage("Starting to commit the changes directly to SQL Server."); using (SqlConnection connection = new SqlConnection(this.ConnectionString)) { connection.Open(); StringBuilder builder = new StringBuilder(); ScriptType[] scriptTypeOrder = new ScriptType[] { ScriptType.Schema, ScriptType.Table, ScriptType.View, ScriptType.Trigger }; foreach (var scriptType in scriptTypeOrder) { //Tables first foreach (var script in scripts.Where(s => s.ScriptType == scriptType)) { builder.AppendLine(script.ScriptText); } } //Other types foreach (var script in scripts.Where(s => !scriptTypeOrder.Contains(s.ScriptType))) { builder.AppendLine(script.ScriptText); } Server server = new Server(new ServerConnection(connection)); try { server.ConnectionContext.BeginTransaction(); server.ConnectionContext.ExecuteNonQuery(builder.ToString()); server.ConnectionContext.CommitTransaction(); } catch { _logger.LogMessage("Transaction rolled back due to exception."); server.ConnectionContext.RollBackTransaction(); throw; } } _logger.LogMessage("Changes have been committed."); }
public void Commit(ChangeScript[] scripts) { _logger.LogMessage("Starting to commit the changes to the output file {0}.", Destination); StringBuilder builder = new StringBuilder(); ScriptType[] scriptTypeOrder = new ScriptType[] { ScriptType.Schema, ScriptType.Table, ScriptType.View, ScriptType.Trigger }; foreach (var scriptType in scriptTypeOrder) { //Tables first foreach (var script in scripts.Where(s => s.ScriptType == scriptType)) { builder.AppendLine(script.ScriptText); } } //Other types foreach (var script in scripts.Where(s => !scriptTypeOrder.Contains(s.ScriptType))) { builder.AppendLine(script.ScriptText); } FileInfo file = new FileInfo(Destination); if (!file.Directory.Exists) { file.Directory.Create(); } if (file.Exists) { file.Delete(); } using (FileStream fileStream = file.OpenWrite()) using (StreamWriter writer = new StreamWriter(fileStream)) { writer.Write(builder.ToString()); } _logger.LogMessage("Finished committing to file {0}.", Destination); }