Exemplo n.º 1
0
        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();
            }
        }
Exemplo n.º 2
0
        public IEnumerable <ImdbMovie> GetMoviesByYear(int year)
        {
            DbConnection dbConnection;
            DbDataReader dbDataReader = null;
            DbCommand    dbCommand    = null;

            try
            {
                dbConnection = CreateDbConnection();
                dbConnection.Open();
                dbCommand    = CommandFactoryMovies.CreateCommandForGetMoviesByYear(dbConnection, year);
                dbDataReader = dbCommand.ExecuteReader(CommandBehavior.CloseConnection);
                return(ModelAdapterMovies.ToImdbMovies(dbDataReader));
            }
            finally
            {
                dbCommand.DisposeIfNotNull();
                // Because we're "Streaming" the results
                // We can't Close/Dispose the dbConnection or the dbDataReader Here
                // Ideally the dbDataReader would be closed after enumerating the results
            }
        }