예제 #1
0
        /// <summary>
        /// Execute the query and return an array of new instances of typed results filled with data table result.
        /// </summary>
        /// <typeparam name="T">Object type</typeparam>
        /// <returns>Array of typed results</returns>
        /// <example>
        /// <code>
        ///   Employee[] emp = cmd.ExecuteTable&lt;Employee&gt;();
        ///   var x = cmd.ExecuteTable&lt;Employee&gt;();
        /// </code>
        /// <remarks>
        ///   Result object property (ex. Employee.Name) may be tagged with the ColumnAttribute
        ///   to set which column name (ex. [Column("Name")] must be associated to this property.
        /// </remarks>
        /// </example>
        public virtual IEnumerable <T> ExecuteTable <T>()
        {
            return(ExecuteInternalCommand(() =>
            {
                using (DbDataReader dr = this.Command.ExecuteReader())
                {
                    // Primitive type: Executable<string>()
                    if (TypeExtension.IsPrimitive(typeof(T)))
                    {
                        return DataReaderConvertor.ToPrimitives <T>(dr);
                    }

                    // Dynamic type: Executable<dynamic>()
                    else if (DynamicConvertor.IsDynamic(typeof(T)))
                    {
                        return DataReaderConvertor.ToDynamic <T>(dr);
                    }

                    // Object type: Executable<Employee>()
                    else
                    {
                        return DataReaderConvertor.ToType <T>(dr).Rows;
                    }
                }
            }));
        }
예제 #2
0
        /// <summary>
        /// Execute the query and return a new instance of T with the first row of results
        /// </summary>
        /// <typeparam name="T">Object type</typeparam>
        /// <returns>First row of results</returns>
        /// <example>
        /// <code>
        ///   Employee emp = cmd.ExecuteRow&lt;Employee&gt;();
        /// </code>
        /// <remarks>
        ///   Result object property (ex. Employee.Name) may be tagged with the ColumnAttribute
        ///   to set which column name (ex. [Column("Name")] must be associated to this property.
        /// </remarks>
        /// </example>
        public virtual T ExecuteRow <T>()
        {
            // Primitive type: Executable<string>()
            if (TypeExtension.IsPrimitive(typeof(T)))
            {
                return(this.ExecuteScalar <T>());
            }
            else
            {
                // Get DataTable
                var rows = ExecuteInternalCommand(() =>
                {
                    using (DbDataReader dr = this.Command.ExecuteReader(System.Data.CommandBehavior.SingleRow))
                    {
                        // Dynamic type: Executable<dynamic>()
                        if (DynamicConvertor.IsDynamic(typeof(T)))
                        {
                            return(DataReaderConvertor.ToDynamic <T>(dr));
                        }

                        // Object type: Executable<Employee>()
                        else
                        {
                            return(DataReaderConvertor.ToType <T>(dr).Rows);
                        }
                    }
                });

                // Return
                return(rows?.Any() == true?rows.First() : default(T));
            }
        }
예제 #3
0
        /// <summary>
        /// Execute the query and return an array of new instances of typed results filled with data table result.
        /// </summary>
        /// <typeparam name="T">Object type</typeparam>
        /// <returns>Array of typed results</returns>
        /// <example>
        /// <code>
        ///   Employee[] emp = cmd.ExecuteTable&lt;Employee&gt;();
        ///   var x = cmd.ExecuteTable&lt;Employee&gt;();
        /// </code>
        /// <remarks>
        ///   Result object property (ex. Employee.Name) may be tagged with the ColumnAttribute
        ///   to set which column name (ex. [Column("Name")] must be associated to this property.
        /// </remarks>
        /// </example>
        public async virtual Task <IEnumerable <T> > ExecuteTableAsync <T>()
        {
            return(await ExecuteInternalCommand(async() =>
            {
                using (DbDataReader dr = await this.Command.ExecuteReaderAsync())
                {
                    // Primitive type: Executable<string>()
                    if (TypeExtension.IsPrimitive(typeof(T)))
                    {
                        return await DataReaderConvertor.ToPrimitivesAsync <T>(dr);
                    }

                    // Dynamic type: Executable<dynamic>()
                    else if (DynamicConvertor.IsDynamic(typeof(T)))
                    {
                        return await DataReaderConvertor.ToDynamicAsync <T>(dr);
                    }

                    // Object type: Executable<Employee>()
                    else
                    {
                        return (await DataReaderConvertor.ToTypeAsync <T>(dr)).Rows;
                    }
                }
            }));
        }