コード例 #1
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
            {
                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));
            }
        }
コード例 #2
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
            {
                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));
            }
        }
コード例 #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"></typeparam>
        /// <param name="converter"></param>
        /// <returns>Array of typed results</returns>
        public virtual IEnumerable <T> ExecuteTable <T>(Func <Schema.DataRow, T> converter)
        {
            Schema.DataTable table = this.ExecuteInternalDataTable(firstRowOnly: false);
            int rowCount           = table.Rows.Length;

            var results = new T[rowCount];

            for (int i = 0; i < rowCount; i++)
            {
                results[i] = converter.Invoke(table.Rows[i]);
            }

            return(results);
        }
コード例 #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));
            }
        }
コード例 #5
0
 /// <summary>
 /// Execute the query and fill the specified T object with the first row of results
 /// </summary>
 /// <typeparam name="T">Object type</typeparam>
 /// <param name="converter"></param>
 /// <returns>First row of results</returns>
 public virtual T ExecuteRow <T>(Func <Schema.DataRow, T> converter)
 {
     if (Convertor.TypeExtension.IsPrimitive(typeof(T)))
     {
         return(this.ExecuteScalar <T>());
     }
     else
     {
         Schema.DataTable table = this.ExecuteInternalDataTable(firstRowOnly: true);
         if (table != null && table.Rows.Length > 0)
         {
             return(converter.Invoke(table.Rows[0]));
         }
         else
         {
             return(default(T));
         }
     }
 }
コード例 #6
0
 /// <summary>
 /// Execute the query and fill the specified T object with the first row of results
 /// </summary>
 /// <typeparam name="T">Object type</typeparam>
 /// <param name="itemOftype"></param>
 /// <returns>First row of results</returns>
 /// <example>
 /// <code>
 ///   Employee emp = new Employee();
 ///   var x = cmd.ExecuteRow(new { emp.Age, emp.Name });
 ///   var y = cmd.ExecuteRow(new { Age = 0, Name = "" });
 ///   var z = cmd.ExecuteRow(emp);
 /// </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>(T itemOftype)
 {
     if (Convertor.TypeExtension.IsPrimitive(typeof(T)))
     {
         return(this.ExecuteScalar <T>());
     }
     else
     {
         Schema.DataTable table = this.ExecuteInternalDataTable(firstRowOnly: true);
         if (table != null && table.Rows.Length > 0)
         {
             return(table.Rows[0].ConvertTo <T>(itemOftype));
         }
         else
         {
             return(default(T));
         }
     }
 }
コード例 #7
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));
            }
        }
コード例 #8
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>()
 {
     Schema.DataTable table = this.ExecuteInternalDataTable(firstRowOnly: false);
     return(table?.ConvertTo <T>());
 }