Esempio n. 1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="database"></param>
        /// <returns></returns>
        public Int32 ExecuteNonQuery(Database database)
        {
            if (database == null)
            {
                throw new ArgumentNullException("database");
            }
            var affectedRecordCount = -1;
            var previousState       = database.ConnectionState;

            try
            {
                var cm = CreateCommand();
                var e  = new StoredProcedureExecutingEventArgs(this, cm);
                StoredProcedure.OnExecuting(e);
                if (e.Cancel == true)
                {
                    return(affectedRecordCount);
                }
                affectedRecordCount = database.ExecuteCommand(cm);
                this.SetOutputParameterValue(cm);
            }
            finally
            {
                if (previousState == ConnectionState.Closed && database.ConnectionState == ConnectionState.Open)
                {
                    database.Close();
                }
                if (previousState == ConnectionState.Closed && database.OnTransaction == false)
                {
                    database.Dispose();
                }
            }
            StoredProcedure.OnExecuted(new StoredProcedureExecutedEventArgs(this));
            return(affectedRecordCount);
        }
Esempio n. 2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="database"></param>
        /// <returns></returns>
        public async Task <List <StoredProcedureResultSet> > GetResultSetsAsync(Database database)
        {
            if (database == null)
            {
                throw new ArgumentNullException("database");
            }
            DbDataReader dr            = null;
            var          previousState = database.ConnectionState;
            var          resultsets    = new List <StoredProcedureResultSet>();

            try
            {
                var cm = CreateCommand(database);
                var e  = new StoredProcedureExecutingEventArgs(this, cm);
                StoredProcedure.OnExecuting(e);
                dr = await database.ExecuteReaderAsync(cm);

                while (dr.Read())
                {
                    var rs = CreateResultSets(dr);
                    resultsets.Add(rs);
                }
                dr.Close();
                this.SetOutputParameterValue(cm);
            }
            finally
            {
                if (dr != null)
                {
                    dr.Dispose();
                }
                if (previousState == ConnectionState.Closed && database.ConnectionState == ConnectionState.Open)
                {
                    database.Close();
                }
                if (previousState == ConnectionState.Closed && database.OnTransaction == false)
                {
                    database.Dispose();
                }
            }
            StoredProcedure.OnExecuted(new StoredProcedureExecutedEventArgs(this));
            return(resultsets);
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="database"></param>
        /// <returns></returns>
        public IEnumerable <StoredProcedureResultSet> EnumerateResultSets(Database database)
        {
            if (database == null)
            {
                throw new ArgumentNullException("database");
            }
            DbDataReader dr            = null;
            var          previousState = database.ConnectionState;

            try
            {
                var cm = CreateCommand();
                var e  = new StoredProcedureExecutingEventArgs(this, cm);
                StoredProcedure.OnExecuting(e);
                dr = database.ExecuteReader(cm);
                while (dr.Read())
                {
                    var rs = CreateResultSets(dr);
                    yield return(rs);
                }
                dr.Close();
                this.SetOutputParameterValue(cm);
            }
            finally
            {
                if (dr != null)
                {
                    dr.Dispose();
                }
                if (previousState == ConnectionState.Closed && database.ConnectionState == ConnectionState.Open)
                {
                    database.Close();
                }
                if (previousState == ConnectionState.Closed && database.OnTransaction == false)
                {
                    database.Dispose();
                }
            }
            StoredProcedure.OnExecuted(new StoredProcedureExecutedEventArgs(this));
        }
Esempio n. 4
0
        private async Task <ExecuteNonQueryResult> GetExecuteNonQueryResultAsync(Database database, CancellationToken cancellationToken)
        {
            if (database == null)
            {
                throw new ArgumentNullException("database");
            }
            var affectedRecordCount = -1;
            var previousState       = database.ConnectionState;

            try
            {
                var cm = CreateCommand(database);
                var e  = new StoredProcedureExecutingEventArgs(this, cm);
                StoredProcedure.OnExecuting(e);
                if (e.Cancel == true)
                {
                    return(new ExecuteNonQueryResult(database, affectedRecordCount));
                }
                affectedRecordCount = await database.ExecuteCommandAsync(cm, cancellationToken).ConfigureAwait(false);

                this.SetOutputParameterValue(cm);
            }
            finally
            {
                if (previousState == ConnectionState.Closed && database.ConnectionState == ConnectionState.Open)
                {
                    database.Close();
                }
                if (previousState == ConnectionState.Closed && database.OnTransaction == false)
                {
                    database.Dispose();
                }
            }
            StoredProcedure.OnExecuted(new StoredProcedureExecutedEventArgs(this));
            return(new ExecuteNonQueryResult(database, affectedRecordCount));
        }