Beispiel #1
0
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="dal"></param>
        /// <returns></returns>
        public IEnumerable <T> ExcuteTransationResult <T>(CommonDal <T> dal, OPERATION OPER) where T : IModel
        {
            if (_ct == null)
            {
                TransactionOptions ops = new TransactionOptions();
                ops.IsolationLevel = _level;
                _ct         = new CommittableTransaction(ops);
                _sqlDicList = new Dictionary <string, SqlConnection>();
            }

            string        ConnStr = dal.getConnString();
            SqlConnection sqlCon  = null;

            foreach (var item in _sqlDicList)
            {
                if (ConnStr == item.Key)
                {
                    sqlCon = item.Value;
                    break;
                }
            }

            if (sqlCon == null)
            {
                sqlCon = new SqlConnection(ConnStr);
                sqlCon.Open();
                _sqlDicList.Add(ConnStr, sqlCon);
                sqlCon.EnlistTransaction(_ct);
            }

            _sqlExe = new SqlExecutor(sqlCon);

            try
            {
                switch (OPER)
                {
                case OPERATION.SELECT:
                    return(dal.ExcuteSelect(_sqlExe));

                case OPERATION.DELETE:
                    return(dal.ExcuteDelete(_sqlExe));

                case OPERATION.INSERT:
                    return(dal.ExcuteInsert(_sqlExe));

                case OPERATION.UPDATE:
                    return(dal.ExcuteUpdate(_sqlExe));

                default:
                    return(dal.ExcuteSelect(_sqlExe));
                }
            }
            catch (Exception ex)
            {
                logger.Error(GetExceptionDetails(ex));
                _ct.Rollback();
                _done = true;
                return(null);
            }
        }
Beispiel #2
0
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="dal"></param>
        /// <returns></returns>
        public static IEnumerable <T> ExcuteResult <T>(CommonDal <T> dal, OPERATION OPER, List <string> field = null) where T : IModel
        {
            var sqlCon = new SqlConnection(dal.getConnString());
            var sqlExe = new SqlExecutor(sqlCon);

            try
            {
                switch (OPER)
                {
                case OPERATION.SELECT:
                    return(dal.ExcuteSelect(sqlExe, field));

                case OPERATION.DELETE:
                    return(dal.ExcuteDelete(sqlExe));

                case OPERATION.INSERT:
                    return(dal.ExcuteInsert(sqlExe));

                case OPERATION.UPDATE:
                    return(dal.ExcuteUpdate(sqlExe));

                default:
                    return(dal.ExcuteSelect(sqlExe));
                }
            }
            catch (Exception ex)
            {
                StringBuilder message = new StringBuilder();
                var           detail  = dal.getParaLogDetail();
                if (detail != null)
                {
                    var dbExisted = dal.getConnString().Split(';').Length > 0;

                    if (dbExisted)
                    {
                        message.AppendLine("連線: " + dal.getConnString().Split(';')[0]);
                    }
                    message.AppendLine("語法: " + dal.getCommandString());
                    message.AppendLine("參數: ");
                    foreach (var item in detail)
                    {
                        message.AppendLine(item.Key + "=>" + item.Value);
                    }
                }
                message.AppendLine(GetExceptionDetails(ex));
                logger.Error(message);
                return(null);
            }
        }