Пример #1
0
        public IEnumerable <IDataReader> Execute(IAdoCommandBuilder builder)
        {
            IDbConnection connection = null;

            try
            {
                connection = this.GetOpenConnection();
            }
            catch (Exception ex)
            {
                this.ApplyConnectionFilters(h => h.OnConnectionException, ex);

                throw new AdoException("An error occurred opening the ADO.NET connection.", ex);
            }

            using IDbCommand dbCommand = connection.CreateCommand();

            try
            {
                builder.Build(dbCommand);
            }
            catch (Exception ex)
            {
                this.ApplyCommandFilters(h => h.OnCommandException, dbCommand, ex);

                throw new AdoException("An error occurred building the ADO.NET command object.", ex);
            }

            this.ApplyCommandFilters(h => h.OnCommandCreated, dbCommand);

            IDataReader reader = null;

            try
            {
                reader = dbCommand.ExecuteReader();
            }
            catch (Exception ex)
            {
                this.ApplyCommandFilters(h => h.OnCommandException, dbCommand, ex);

                throw new AdoException("An error occurred executing the ADO.NET command.", ex);
            }

            try
            {
                do
                {
                    yield return(reader);
                }while (reader.NextResult());
            }
            finally
            {
                reader?.Dispose();
            }

            this.ApplyCommandFilters(h => h.OnCommandExecuted, dbCommand);
        }
Пример #2
0
        public async Task ExecuteAsync(IAdoCommandBuilder builder, Func <DbDataReader, Task> consumer, CancellationToken cancellationToken)
        {
            DbConnection connection = await this.GetOpenConnectionAsync(cancellationToken).ConfigureAwait(false);

            using DbCommand dbCommand = connection.CreateCommand();

            try
            {
                builder.Build(dbCommand);
            }
            catch (Exception ex)
            {
                this.ApplyCommandFilters(h => h.OnCommandException, dbCommand, ex);

                throw new AdoException("An error occurred building the ADO.NET command object.", ex);
            }

            this.ApplyCommandFilters(h => h.OnCommandCreated, dbCommand);

            if (cancellationToken.IsCancellationRequested)
            {
                cancellationToken.ThrowIfCancellationRequested();
            }

            DbDataReader reader = null;

            try
            {
                reader = await dbCommand.ExecuteReaderAsync(cancellationToken).ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                this.ApplyCommandFilters(h => h.OnCommandException, dbCommand, ex);

                throw new AdoException("An error occurred executing the ADO.NET command.", ex);
            }

            try
            {
                do
                {
                    if (cancellationToken.IsCancellationRequested)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                    }

                    await consumer(reader).ConfigureAwait(false);
                } while (await reader.NextResultAsync(cancellationToken).ConfigureAwait(false));
            }
            finally
            {
                reader?.Dispose();
            }

            this.ApplyCommandFilters(h => h.OnCommandExecuted, dbCommand);
        }