Example #1
0
        public SpResultException(SpExecResult spExecResult)
        {
            if (spExecResult == null)
            {
                // SP execution result not defined
                throw new ArgumentNullException("spExecResult");
            }

            ReturnCode   = spExecResult.ReturnCode;
            SpName       = spExecResult.StoredProcedureName;
            ErrorMessage = spExecResult.ErrorMessage;
        }
        protected override async Task <object> ProcessDbCommandAsync(SqlCommand dbCommand)
        {
            SpExecResult result = new SpExecResult();

            SqlDataReader reader = null;

            try
            {
                // set stored procedure name
                result.StoredProcedureName = DbContext.GetFullStoredProcName(GetCommandText());

                // execute the stored procedure
                reader = await dbCommand.ExecuteReaderAsync();

                if (!reader.HasRows)
                {
                    // if the stored procedure did not
                    // return any row
                    reader.Close();
                    result.ReturnCode = OutReturnCode;
                    return(result);
                }

                IModelBuilder <TResult> builder = CreateBuilder();

                while (await reader.ReadAsync())
                {
                    TResult newModel = builder.Build(reader);
                    result.AppendRow(newModel);
                }

                // close the reader
                reader.Close();

                // set return code
                result.ReturnCode = OutReturnCode;
                return(result);
            }
            catch (Exception e)
            {
                throw;
            }
            finally
            {
                if ((reader != null) && !reader.IsClosed)
                {
                    // close the data reader if it was not closed
                    reader.Close();
                }
            }
        }
        protected override async Task <object> ProcessDbCommandAsync(SqlCommand dbCommand)
        {
            // execute the stored procedure and return the response code
            SpExecResult execResult = new SpExecResult();

            // execute the stored procedure
            try
            {
                // set the name of just executed stored procedure
                execResult.StoredProcedureName = DbContext.GetFullStoredProcName(GetCommandText());

                // execute the stored procedure
                await dbCommand.ExecuteNonQueryAsync();

                // get the return code from the @ReturnVal parameter
                execResult.ReturnCode = OutReturnCode;

                // set the error message from @ErrorMessage parameter.
                // If no error occured, then return NULL
                execResult.ErrorMessage = ErrorMessage(dbCommand);

                if ((execResult.ReturnCode != SpExecResult.NoErrorsCode) && ThrowErrorOnNonSuccessCode)
                {
                    // throw error here in case the stored procedure
                    // returned a non-success code
                    throw new SpResultException(execResult);
                }
            }
            catch (SqlException sqlEx)
            {
                throw;
                // todo: log error here
            }
            catch (Exception e)
            {
                throw;
            }

            // return (-1) to denote that it's impossible to
            // return the actual return code since the @ReturnVal parameter
            // was not provided
            return(execResult);
        }