Example #1
0
        public int Execute(string sql, String[] paramNames, Object[] paramValues, string dbName, CommandType pCmdType = CommandType.Text)
        {
            using (Connection = DBUtil.GetConnection(dbName))
            {
                try
                {
                    Connection.Open();
                    this.dbType      = ConfigCache.GetDBType(dbName);
                    this.Transaction = this.Connection.BeginTransaction();
                    int result = Execute(sql, paramNames, paramValues, pCmdType);
                    this.Transaction.Commit();

                    return(result);
                }
                catch (Exception e)
                {
                    this.Transaction.Rollback();
                    throw e;
                }
                finally
                {
                    if (Connection != null && ConnectionState.Open == Connection.State)
                    {
                        Connection.Close();
                    }
                }
            }
        }
Example #2
0
 public DataTable Query(string sql, String[] paramNames, Object[] paramValues, string dbName)
 {
     using (Connection = DBUtil.GetConnection(dbName))
     {
         try
         {
             Connection.Open();
             this.dbType = ConfigCache.GetDBType(dbName);
             return(Query(sql, paramNames, paramValues));
         }
         finally
         {
             if (Connection != null && ConnectionState.Open == Connection.State)
             {
                 Connection.Close();
             }
         }
     }
 }
Example #3
0
        public void Intercept(IInvocation invocation)
        {
            if (count > 0)
            {
                // 嵌套调用
                invocation.Proceed();
                return;
            }

            count++;
            IDbTransaction transaction = null;
            string         DBName      = "";

            object[] attrs = invocation.TargetType.GetCustomAttributes(typeof(DBSourceAttribute), false);
            if (attrs != null && attrs.Length > 0)
            {
                DBName = (attrs[0] as DBSourceAttribute).name;
            }

            //short dbType = 0;
            short dbType = ConfigCache.GetDBType(DBName);

            // 从类中指定,
            // 从方法中指定
            if (connectString == null)
            {
                //connectString = System.Configuration.ConfigurationManager.ConnectionStrings["CONNECTION_STRING"].ConnectionString;
                connectString = ConfigCache.GetDBConnectStr(DBName);

                //connectString = "Data Source=172.30.1.65:1522//xe;;user id=etl;password=etl;";
                //connectString = "Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=Virgil-Chen)(PORT=1522)))(CONNECT_DATA=(SERVICE_NAME=XE)));User Id=czjd_sd;Password=czjd_sd;";
                //connectString = "Data Source=Virgil-PC:1522//xe;;user id=czjd_sd;password=czjd_sd;";
                //connectString = "server=localhost;User Id=root;database=sakila;Password=root;";
            }

            using (IDbConnection connection = DBUtil.GetConnection(dbType, connectString))
            {
                try
                {
                    connection.Open();

                    string methodName = invocation.MethodInvocationTarget.Name.ToLower();
                    if (!(methodName.StartsWith("get") ||
                          methodName.StartsWith("find") ||
                          methodName.StartsWith("query") ||
                          methodName.StartsWith("list")))
                    {
                        if (log.IsDebugEnabled)
                        {
                            log.Debug("BeginTransaction");
                        }
                        transaction = connection.BeginTransaction();
                    }

                    new TransactionContext(connection, transaction, dbType);

                    invocation.Proceed();

                    if (transaction != null)
                    {
                        transaction.Commit();
                    }
                }
                catch (Exception e)
                {
                    if (transaction != null)
                    {
                        transaction.Rollback();
                    }

                    log.Error(e.Message, e);

                    throw e;
                }
                finally
                {
                    if (log.IsDebugEnabled)
                    {
                        log.Debug("connection.Close()");
                    }

                    if (connection != null && ConnectionState.Open == connection.State)
                    {
                        connection.Close();
                    }
                    count--;
                }
            }
        }