Esempio n. 1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="request"></param>
        /// <param name="rowAffect"></param>
        /// <returns></returns>
        /// <exception cref="ApplicationException">You should set KeepConnection is True</exception>
        /// <exception cref="DataAccessException"><c>DataAccessException</c>.</exception>
        public override ParameterCollection ExecutePackage(DataRequest request, out int rowAffect)
        {
            //เก็บ Parameter ที่มี Direction : IN/OUT , OUT, RETURN
            ParameterCollection ret = new ParameterCollection();

            //เช็คการใช้งาน Transaction
            if (DBTransactionState == TransactionState.PROCESSING)
            {
                //Transaction ทำงานเป็น Non-Connectionless จึงต้องเปิด Connection ไว้เสมอ
                // จนกว่าจะ Commit / Rollback  จึงจะสามารถปิด Connection ได้
                // ดังนั้นจึงจะต้องกำหนด KeepConnection = True
                if (!KeepConnection)
                {
                    throw new ApplicationException("You should set KeepConnection is True");
                }

                //Connection จะต้อง Open ไว้แล้ว  เนื่องจากมีการใช้ Transaction
            }
            else
            {
                //ถ้าไม่มีการใช้ Transaction จะต้อง ReOpen connection
                if (DBConnectionState == ConnectionState.Open)
                {
                    this.Close();
                }
                this.Open();
            }

            request.CommandType = CommandType.StoredProcedure;
            SqlCommand cmd = (SqlCommand)CreateCommand(request);

            try
            {
                rowAffect = cmd.ExecuteNonQuery();
            }
            catch (Exception err)
            {
                //Save db exception
                //byte[] bytes = request.SerializeObject();
                //DBExceptionDAO.AddNew(bytes, ex.StackTrace, ex.Message);

                DataAccessException ex = new DataAccessException(err.Message, err);
                ex.DataRequest = request;
                throw ex;
            }
            finally
            {
                //Close connection when not use transaction and KeepConnection is false.
                if (DBTransactionState == TransactionState.IDLE)
                {
                    if (!KeepConnection)
                    {
                        this.Close();
                    }
                }
            }

            for (int i = 0; i < request.Parameters.Count; i++)
            {
                switch (request.Parameters[i].Direction)
                {
                case ParameterDirection.InputOutput:
                case ParameterDirection.Output:
                case ParameterDirection.ReturnValue:
                    Parameter p = (Parameter)request.Parameters[i].Clone();
                    p.Value = cmd.Parameters[p.Name].Value;
                    ret.Add(p);
                    break;
                }
            }


            return(ret);
        }
Esempio n. 2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public override ParameterCollection ExecutePackage(DataRequest request)
        {
            int iRowAffected = 0;

            return(ExecutePackage(request, out iRowAffected));
        }
Esempio n. 3
0
        /// <exception cref="DataAccessException"><c>DataAccessException</c>.</exception>
        public override ParameterCollection ExecutePackage(DataRequest request, out int rowAffect)
        {
            //à¡çº Parameter ·ÕèÁÕ Direction : IN/OUT , OUT, RETURN
            ParameterCollection ret = new ParameterCollection();

            //àªç¤¡ÒÃãªé§Ò¹ Transaction
            if (DBTransactionState == TransactionState.PROCESSING)
            {
                //Transaction ·Ó§Ò¹à»ç¹ Non-Connectionless ¨Ö§µéͧà»Ô´ Connection äÇéàÊÁÍ
                // ¨¹¡ÇèҨРCommit / Rollback  ¨Ö§¨ÐÊÒÁÒö»Ô´ Connection ä´é
                // ´Ñ§¹Ñ鹨֧¨Ðµéͧ¡Ó˹´ KeepConnection = True
                if (!KeepConnection)
                {
                    throw new ApplicationException("You should set KeepConnection is True");
                }

                //Connection ¨Ðµéͧ Open äÇéáÅéÇ  à¹×èͧ¨Ò¡ÁÕ¡ÒÃãªé Transaction
            }
            else
            {
                //¶éÒäÁèÁÕ¡ÒÃãªé Transaction ¨Ðµéͧ ReOpen connection
                if (DBConnectionState == ConnectionState.Open)
                {
                    this.Close();
                }
                this.Open();
            }

            request.CommandType = CommandType.StoredProcedure;
            OracleCommand cmd = (OracleCommand)CreateCommand(request);

            try
            {
                rowAffect = cmd.ExecuteNonQuery();
            }
            catch (Exception err)
            {
                //Save db exception
                //byte[] bytes = request.SerializeObject();
                //DBExceptionDAO.AddNew(bytes, ex.StackTrace, ex.Message);

                DataAccessException ex = new DataAccessException(err.Message, err);
                ex.DataRequest = request;
                throw ex;
            }
            finally
            {
                //Close connection when not use transaction and KeepConnection is false.
                if (DBTransactionState == TransactionState.IDLE)
                {
                    if (!KeepConnection)
                    {
                        this.Close();
                    }
                }
            }

            for (int i = 0; i < request.Parameters.Count; i++)
            {
                switch (request.Parameters[i].Direction)
                {
                case ParameterDirection.InputOutput:
                case ParameterDirection.Output:
                case ParameterDirection.ReturnValue:
                    Parameter p = (Parameter)request.Parameters[i].Clone();
                    p.Value = cmd.Parameters[p.Name].Value;
                    ret.Add(p);
                    break;
                }
            }


            return(ret);
        }
Esempio n. 4
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        /// <exception cref="ApplicationException">You should set KeepConnection to True</exception>
        /// <exception cref="DataAccessException"><c>DataAccessException</c>.</exception>
        public override DataTable ExecuteQuery(DataRequest request)
        {
            DataTable      resultDT = null;
            SqlDataAdapter adapt    = null;

            //คล้ายคำสั่ง ExecuteDataReader  แต่จะเก็บลง DataSet
            //Mayby transaction
            if (DBTransactionState == TransactionState.PROCESSING)
            {
                //Transaction ทำงานเป็น Non-Connectionless จึงต้องเปิด Connection ไว้เสมอ
                // จนกว่าจะ Commit / Rollback  จึงจะสามารถปิด Connection ได้
                // ดังนั้นจึงจะต้องกำหนด KeepConnection = True
                if (!KeepConnection)
                {
                    throw new ApplicationException("You should set KeepConnection to True");
                }

                //Connection จะต้อง Open ไว้แล้ว  เนื่องจากมีการใช้ Transaction
            }
            else
            {
                if (KeepConnection)
                {
                    if (DBConnectionState == ConnectionState.Closed)
                    {
                        this.Open();
                    }
                }
                else
                {
                    //ถ้าไม่มีการใช้ Transaction จะต้อง ReOpen connection
                    if (DBConnectionState == ConnectionState.Open)
                    {
                        this.Close();
                    }
                    this.Open();
                }
            }

            try
            {
                adapt = new SqlDataAdapter();
                adapt.SelectCommand = (SqlCommand)CreateCommand(request);
                resultDT            = new DataTable(request.ResultTableName);
                adapt.Fill(resultDT);
            }
            catch (Exception err)
            {
                DataAccessException ex = new DataAccessException(err.Message, err);
                ex.DataRequest = request;
                throw ex;
            }

            //Close connection when not use transaction and KeepConnection is false.
            if (DBTransactionState == TransactionState.IDLE)
            {
                if (!KeepConnection)
                {
                    this.Close();
                }
            }

            return(resultDT);
        }
Esempio n. 5
0
        /// <exception cref="DataAccessException"><c>DataAccessException</c>.</exception>
        public override DataTable ExecuteQuery(DataRequest request)
        {
            DataTable         resultDT = null;
            OracleDataAdapter adapt    = null;

            //¤ÅéÒ¤ÓÊÑè§ ExecuteDataReader  áµè¨Ðà¡çºÅ§ DataSet
            //Mayby transaction
            if (DBTransactionState == TransactionState.PROCESSING)
            {
                //Transaction ·Ó§Ò¹à»ç¹ Non-Connectionless ¨Ö§µéͧà»Ô´ Connection äÇéàÊÁÍ
                // ¨¹¡ÇèҨРCommit / Rollback  ¨Ö§¨ÐÊÒÁÒö»Ô´ Connection ä´é
                // ´Ñ§¹Ñ鹨֧¨Ðµéͧ¡Ó˹´ KeepConnection = True
                if (!KeepConnection)
                {
                    throw new ApplicationException("You should set KeepConnection to True");
                }

                //Connection ¨Ðµéͧ Open äÇéáÅéÇ  à¹×èͧ¨Ò¡ÁÕ¡ÒÃãªé Transaction
            }
            else
            {
                if (KeepConnection)
                {
                    if (DBConnectionState == ConnectionState.Closed)
                    {
                        this.Open();
                    }
                }
                else
                {
                    //¶éÒäÁèÁÕ¡ÒÃãªé Transaction ¨Ðµéͧ ReOpen connection
                    if (DBConnectionState == ConnectionState.Open)
                    {
                        this.Close();
                    }
                    this.Open();
                }
            }

            try {
                adapt = new OracleDataAdapter();
                adapt.SelectCommand = (OracleCommand)CreateCommand(request);
                resultDT            = new DataTable(request.ResultTableName);
                adapt.Fill(resultDT);
            } catch (Exception err) {
                DataAccessException ex = new DataAccessException(err.Message, err);
                ex.DataRequest = request;
                throw ex;
            }

            //Close connection when not use transaction and KeepConnection is false.
            if (DBTransactionState == TransactionState.IDLE)
            {
                if (!KeepConnection)
                {
                    this.Close();
                }
            }

            return(resultDT);
        }