/// <summary>
        /// Execute the query and return the count of modified rows
        /// </summary>
        /// <returns>Count of modified rows</returns>
        public virtual int ExecuteNonQuery()
        {
            ResetException();

            try
            {
                Update_CommandDotCommandText_If_CommandText_IsNew();

                // Action Before Execution
                if (this.ActionBeforeExecution != null)
                {
                    this.ActionBeforeExecution.Invoke(this);
                    Update_CommandDotCommandText_If_CommandText_IsNew();
                }

                // Replace null parameters by DBNull value.
                this.Replace_ParametersNull_By_DBNull();

                // Log
                if (this.Log != null)
                {
                    this.Log.Invoke(this.Command.CommandText);
                }

                // Send the request to the Database server
                int rowsAffected = 0;

                if (this.Command.CommandText.Length > 0)
                {
                    if (Retry.IsActivated())
                    {
                        rowsAffected = Retry.ExecuteCommandOrRetryIfErrorOccured(() => this.Command.ExecuteNonQuery());
                    }
                    else
                    {
                        rowsAffected = this.Command.ExecuteNonQuery();
                    }
                }

                // Action After Execution
                if (this.ActionAfterExecution != null)
                {
                    var tables = new Schema.DataTable[]
                    {
                        new Schema.DataTable("ExecuteNonQuery", "Result", rowsAffected)
                    };
                    this.ActionAfterExecution.Invoke(this, tables);
                    int?newValue = tables[0].Rows[0][0] as int?;
                    rowsAffected = newValue.HasValue ? newValue.Value : 0;
                }

                return(rowsAffected);
            }
            catch (DbException ex)
            {
                return(ThrowSqlExceptionOrDefaultValue <int>(ex));
            }
        }
        /// <summary>
        /// Execute the query and return the first column of the first row of results
        /// </summary>
        /// <returns>Object - Result</returns>
        public virtual object ExecuteScalar()
        {
            ResetException();

            try
            {
                Update_CommandDotCommandText_If_CommandText_IsNew();

                // Action Before Execution
                if (this.ActionBeforeExecution != null)
                {
                    this.ActionBeforeExecution.Invoke(this);
                    Update_CommandDotCommandText_If_CommandText_IsNew();
                }

                // Replace null parameters by DBNull value.
                this.Replace_ParametersNull_By_DBNull();

                // Log
                if (this.Log != null)
                {
                    this.Log.Invoke(this.Command.CommandText);
                }

                // Send the request to the Database server
                object result = null;

                if (this.Command.CommandText.Length > 0)
                {
                    if (Retry.IsActivated())
                    {
                        result = Retry.ExecuteCommandOrRetryIfErrorOccured(() => this.Command.ExecuteScalar());
                    }
                    else
                    {
                        result = this.Command.ExecuteScalar();
                    }
                }

                // Action After Execution
                if (this.ActionAfterExecution != null)
                {
                    var tables = new Schema.DataTable[]
                    {
                        new Schema.DataTable("ExecuteScalar", "Result", result)
                    };
                    this.ActionAfterExecution.Invoke(this, tables);
                    result = tables[0].Rows[0][0];
                }

                return(result);
            }
            catch (DbException ex)
            {
                return(ThrowSqlExceptionOrDefaultValue <object>(ex));
            }
        }
Exemple #3
0
        /// <summary>
        /// Execute the query and return an internal DataTable with all data.
        /// </summary>
        /// <param name="firstRowOnly"></param>
        /// <returns></returns>
        internal virtual IEnumerable <Schema.DataTable> ExecuteInternalDataSet(bool firstRowOnly)
        {
            ResetException();

            try
            {
                Update_CommandDotCommandText_If_CommandText_IsNew();

                // Action Before Execution
                if (this.ActionBeforeExecution != null)
                {
                    this.ActionBeforeExecution.Invoke(this);
                    Update_CommandDotCommandText_If_CommandText_IsNew();
                }

                // Log
                if (this.Log != null)
                {
                    this.Log.Invoke(this.Command.CommandText);
                }

                var tables = new List <Schema.DataTable>();

                // Send the request to the Database server
                if (this.Command.CommandText.Length > 0)
                {
                    Retry.ExecuteCommandOrRetryIfErrorOccured <bool>(() =>
                    {
                        using (DbDataReader dr = this.Command.ExecuteReader())
                        {
                            do
                            {
                                tables.Add(new Schema.DataTable(dr, firstRowOnly));
                            }while (!firstRowOnly && dr.NextResult());
                        }
                        return(true);
                    });
                }
                else
                {
                    tables.Add(new Schema.DataTable());
                }

                // Action After Execution
                if (this.ActionAfterExecution != null)
                {
                    this.ActionAfterExecution.Invoke(this, tables);
                }

                return(tables.ToArray());
            }
            catch (DbException ex)
            {
                return(ThrowSqlExceptionOrDefaultValue <IEnumerable <Schema.DataTable> >(ex));
            }
        }
Exemple #4
0
        /// <summary>
        /// Execute the query and return the count of modified rows
        /// </summary>
        /// <returns>Count of modified rows</returns>
        public virtual int ExecuteNonQuery()
        {
            ResetException();

            try
            {
                // Commom operations before execution
                this.OperationsBeforeExecution();

                // Send the request to the Database server
                int rowsAffected = 0;

                if (this.Command.CommandText.Length > 0)
                {
                    if (Retry.IsActivated())
                    {
                        rowsAffected = Retry.ExecuteCommandOrRetryIfErrorOccured(() => this.Command.ExecuteNonQuery());
                    }
                    else
                    {
                        rowsAffected = this.Command.ExecuteNonQuery();
                    }
                }

                // Action After Execution
                if (this.ActionAfterExecution != null)
                {
                    var tables = new Schema.DataTable[]
                    {
                        new Schema.DataTable("ExecuteNonQuery", "Result", rowsAffected)
                    };
                    this.ActionAfterExecution.Invoke(this, tables);
                    int?newValue = tables[0].Rows[0][0] as int?;
                    rowsAffected = newValue.HasValue ? newValue.Value : 0;
                }

                return(rowsAffected);
            }
            catch (DbException ex)
            {
                return(ThrowSqlExceptionOrDefaultValue <int>(ex));
            }
        }
Exemple #5
0
        /// <summary>
        /// Execute the query and return the first column of the first row of results
        /// </summary>
        /// <returns>Object - Result</returns>
        public virtual object ExecuteScalar()
        {
            ResetException();

            try
            {
                // Commom operations before execution
                this.OperationsBeforeExecution();

                // Send the request to the Database server
                object result = null;

                if (this.Command.CommandText.Length > 0)
                {
                    if (Retry.IsActivated())
                    {
                        result = Retry.ExecuteCommandOrRetryIfErrorOccured(() => this.Command.ExecuteScalar());
                    }
                    else
                    {
                        result = this.Command.ExecuteScalar();
                    }
                }

                // Action After Execution
                if (this.ActionAfterExecution != null)
                {
                    var tables = new Schema.DataTable[]
                    {
                        new Schema.DataTable("ExecuteScalar", "Result", result)
                    };
                    this.ActionAfterExecution.Invoke(this, tables);
                    result = tables[0].Rows[0][0];
                }

                return(result);
            }
            catch (DbException ex)
            {
                return(ThrowSqlExceptionOrDefaultValue <object>(ex));
            }
        }