Exemple #1
0
        ///Execute are the main methods of <see cref="AdoTemplate"/> class.
        ///All other methods will finally pass through one of
        ///<see cref="AdoTemplate.Execute{T}(IDataAdapterCallback{T})"/>
        ///or
        ///<see cref="AdoTemplate.Execute{T}(IDbCommandCallback{T})"/> methods.
        /// Direct use of this method is not recommended.
        /// <summary>
        ///Execute a ADO.NET operation on a command object using a generic interface based callback.
        /// </summary>
        /// <typeparam name="T">return type</typeparam>
        /// <param name="action">the action to be executed</param>
        /// <returns></returns>
        public T Execute <T>(IDbCommandCallback <T> action)
        {
            IDbCommand command = null;

            try
            {
                DbProvider.OpenConnection();
                command             = DbProvider.CreateCommand();
                command.Connection  = DbProvider.Connection;
                command.Transaction = DbProvider.Transaction;

                T result = default(T);
                result = action.DoInCommand(command);

                return(result);
            }
            catch (DataException)
            {
                throw;
            }
            catch (DbException e)
            {
                throw new DataException($"Failed to execute a command cllback: {e.Message}", e);
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                ConnectionUtils.DisposeCommand(command);
                ConnectionUtils.ClosseConnection(DbProvider);
            }
        }