public async Task CreateMovies(IEnumerable <Movie> movies) { var dbConnection = CreateDbConnection(); DbTransaction dbTransaction = null; DbCommand dbCommand = null; try { await dbConnection.OpenAsync().ConfigureAwait(false); dbTransaction = dbConnection.BeginTransaction(); dbCommand = CommandFactoryMovies.CreateCommandForCreateMoviesTvpDistinctInsertInto(dbConnection, dbTransaction, movies); await dbCommand.ExecuteNonQueryAsync().ConfigureAwait(false); dbTransaction.Commit(); } catch (DbException) { dbTransaction.RollbackIfNotNull(); throw; } finally { dbCommand.DisposeIfNotNull(); dbTransaction.DisposeIfNotNull(); dbConnection.DisposeIfNotNull(); } }
public void CreateMoviesWithoutTvp(IEnumerable <ImdbMovie> imdbMovies) { DbConnection dbConnection = null; DbTransaction dbTransaction = null; DbCommand dbCommand = null; try { dbConnection = CreateDbConnection(); dbConnection.Open(); dbTransaction = dbConnection.BeginTransaction(); foreach (var imdbMovie in imdbMovies) { dbCommand = CommandFactoryMovies.CreateCommandForCreateMovie(dbConnection, dbTransaction, imdbMovie); dbCommand.ExecuteNonQuery(); } dbTransaction.Commit(); } catch (DbException) { dbTransaction.RollbackIfNotNull(); throw; } finally { dbCommand.DisposeIfNotNull(); dbTransaction.DisposeIfNotNull(); dbConnection.CloseAndDispose(); } }
public async Task <int> CreateMovie(Movie movie) { var dbConnection = CreateDbConnection(); DbCommand dbCommand = null; DbTransaction dbTransaction = null; try { await dbConnection.OpenAsync().ConfigureAwait(false); dbTransaction = dbConnection.BeginTransaction(IsolationLevel.Serializable); dbCommand = CommandFactoryMovies.CreateCommandForCreateMovie(dbConnection, dbTransaction, movie); await dbCommand.ExecuteNonQueryAsync().ConfigureAwait(false); dbTransaction.Commit(); return((int)dbCommand.Parameters[0].Value); } catch (DbException e) { dbTransaction.RollbackIfNotNull(); if (e.Message.Contains("duplicate key row in object 'dbo.Movie'", StringComparison.OrdinalIgnoreCase)) { throw new DuplicateMovieException($"A Movie with the Title: {movie.Title} already exists. Please use a different title", e); } else { throw; } } finally { dbCommand.DisposeIfNotNull(); dbTransaction.DisposeIfNotNull(); dbConnection.Dispose(); } }