Skip to content

Instantly share code, notes, and snippets.

@Tigraine
Created July 18, 2011 10:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Tigraine/1089096 to your computer and use it in GitHub Desktop.
Save Tigraine/1089096 to your computer and use it in GitHub Desktop.
Arrow-Head anti-pattern in perfection..
private void CreateSchema()
{
using (var sqlConnection = new SqlConnection(connectionString))
{
sqlConnection.Open();
using (var tx = sqlConnection.BeginTransaction())
{
foreach(var dto in dtos)
{
try
{
using (var command = sqlConnection.CreateCommand())
{
command.Transaction = tx;
var sb = new StringBuilder();
sb.AppendLine(string.Format("IF EXISTS(SELECT * FROM sysobjects WHERE name='{0}' AND xtype = 'U') RETURN;", dto.Name));
sb.AppendLine(builder.CreateSqlCreateStatementFromDto(dto));
command.CommandText = sb.ToString();
command.ExecuteNonQuery();
}
}
catch (Exception ex)
{
Console.WriteLine("Could not create DtoTable: {0}\nMessage: {1}\nStackTrace: {2}", dto.FullName, ex.Message, ex.StackTrace);
tx.Rollback();
throw;
}
}
tx.Commit();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment