コード例 #1
0
        /// <summary>
        /// Executes the reader as paging query result.
        /// </summary>
        /// <typeparam name="TResult">The type of the result.</typeparam>
        /// <typeparam name="TStartIndex">The type of the start index.</typeparam>
        /// <typeparam name="TObject">The type of the object.</typeparam>
        /// <param name="spName">Name of the sp.</param>
        /// <param name="sqlParameters">The SQL parameters.</param>
        /// <param name="converter">The converter.</param>
        /// <param name="preferReadOnlyOperator">if set to <c>true</c> [prefer read only operator].</param>
        /// <returns></returns>
        protected TResult ExecuteReaderAsPagingQueryResult <TResult, TStartIndex, TObject>(string spName, List <SqlParameter> sqlParameters, Func <SqlDataReader, TObject> converter, bool preferReadOnlyOperator = false)
            where TResult : PagingQueryResult <TStartIndex, TObject>, new()
        {
            SqlDataReader    reader           = null;
            DatabaseOperator databaseOperator = null;

            try
            {
                converter.CheckNullObject(nameof(converter));

                reader = Execute(spName, sqlParameters, preferReadOnlyOperator, out databaseOperator);
                var totalCount = reader.HasColumn(column_TotalCount) ? reader[column_TotalCount].ObjectToInt32() : 0;
                return(reader == null ? new TResult() : ConvertQueryResultObject <TResult, TStartIndex, TObject>(reader, totalCount, converter));
            }
            catch (Exception ex)
            {
                throw ex.Handle(new { SpName = spName, Parameters = SqlParameterToList(sqlParameters), PreferReadOnlyOperator = preferReadOnlyOperator });
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                }

                // use Close instead of Dispose so that operator can be reuse without re-initialize.
                databaseOperator?.Close();
            }
        }
コード例 #2
0
        /// <summary>
        /// Executes the reader.
        /// </summary>
        /// <typeparam name="TOutput">The type of the t output.</typeparam>
        /// <param name="spName">Name of the sp.</param>
        /// <param name="sqlParameters">The SQL parameters.</param>
        /// <param name="converter">The converter.</param>
        /// <param name="preferReadOnlyOperator">The prefer read only operator.</param>
        /// <returns>List&lt;TOutput&gt;.</returns>
        protected List <TOutput> ExecuteReader <TOutput>(string spName, List <SqlParameter> sqlParameters, Func <SqlDataReader, TOutput> converter, bool preferReadOnlyOperator = false)
        {
            SqlDataReader    reader           = null;
            DatabaseOperator databaseOperator = null;

            try
            {
                converter.CheckNullObject(nameof(converter));

                reader = Execute(spName, sqlParameters, preferReadOnlyOperator, out databaseOperator);
                return(reader == null ? new List <TOutput>() : ConvertObject(reader, converter));
            }
            catch (Exception ex)
            {
                throw ex.Handle(new { SpName = spName, Parameters = SqlParameterToList(sqlParameters), PreferReadOnlyOperator = preferReadOnlyOperator });
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                }

                // use Close instead of Dispose so that operator can be reuse without re-initialize.
                databaseOperator?.Close();
            }
        }