Пример #1
0
        /// <summary>
        /// 根据数据库的逻辑名称创建Database实例
        /// </summary>
        /// <param name="name">数据库逻辑名称</param>
        /// <returns>Database实例</returns>
        public static Database Create(string name)
        {
            ExceptionHelper.CheckStringIsNullOrEmpty(name, "name");

            //// 根据数据驱动名称对应的数据库实体
            //// 如果以后需要开放DeluxeWorks.Db提供自动加载第三方Database类型的话,也仅需要修改这个方法
            //switch (DbConnectionManager.GetDbProviderName(name))
            //{
            //    case "System.Data.SqlClient":
            //        return new SqlDatabase(name);
            //    case "System.Data.OracleClient":
            //        return new OracleDatabase(name);
            //    //case "Oracle.DataAccess.Client":
            //    //    return new ODP.OracleDatabase(name);
            //    default:
            //        throw new NotSupportedException(name + " 所定义的数据驱动不支持");
            //}

            return(DbConnectionManager.GetDataProvider(name) as Database);
        }
Пример #2
0
        /// <summary>
        /// 根据名称得到当前引用的连接
        /// </summary>
        /// <param name="connName">数据库连接名称</param>
        /// <returns>连接对象</returns>
        protected ReferenceConnection GetRefConnectionWithoutTx(string connName)
        {
            ReferenceConnection result = null;

            DoSafeConnectionOp(connName,
                               this.GraphWithoutTx,
                               (refConnection) =>
            {
                refConnection.ReferenceCount++;
                result = refConnection;
            },
                               () =>
            {
                DbConnection connection   = DbConnectionManager.GetConnection(connName);
                this._isConnectionCreator = true;

                result = new ReferenceConnection(connName, connection);
                this.GraphWithoutTx.Add(connName, result);
            });

            return(result);
        }
Пример #3
0
        /// <summary>
        /// 获取一个DbContext对象
        /// </summary>
        /// <param name="name">连接名称</param>
        /// <param name="autoClose">是否自动关闭</param>
        /// <returns>DbContext对象</returns>
        public static DbContext GetContext(string name, bool autoClose)
        {
            //得到映射后连接名称
            name = DbConnectionMappingContext.GetMappedConnectionName(name);

            DbProviderFactory factory = DbConnectionManager.GetDbProviderFactory(name);

            DbConnectionStringBuilder csb = factory.CreateConnectionStringBuilder();

            csb.ConnectionString = DbConnectionManager.GetConnectionString(name);

            bool enlist = true;

            if (csb.ContainsKey("enlist"))
            {
                enlist = (bool)csb["enlist"];
            }

            DbContext result = null;

            if (enlist)
            {
                result = new AutoEnlistDbContext(name, autoClose);
            }
            else
            {
                result = new NotEnlistDbContext(name, autoClose);
            }

            result.InitDbContext(name, autoClose);

            result._autoClose      = autoClose;
            result._commandTimeout = DbConnectionManager.GetCommandTimeout(name);

            return(result);
        }
Пример #4
0
        private async Task <Tuple <DbConnection, bool> > CreateConnectionAsync(string name)
        {
            bool isConnectionCreator = false;

            // non-transactional operation
            GraphWithoutTransaction connections = this.GraphWithoutTx;

            DbConnection connection = GetConnection(name);

            if ((connection != null) && (connection.State != ConnectionState.Open))
            {
                if (connection.ConnectionString.IsNullOrEmpty())
                {
                    connection.ConnectionString = DbConnectionManager.GetConnectionString(name);
                }

                await OpenConnectionAsync(name, connection);

                WriteTraceInfo(connection.DataSource + "." + connection.Database
                               + "[" + SNTPClient.AdjustedTime.ToString("yyyyMMdd HH:mm:ss.fff") + "]", " Open Connection ");
            }

            return(new Tuple <DbConnection, bool>(connection, isConnectionCreator));
        }
Пример #5
0
        /// <summary>
        /// 根据名称得到当前引用的连接
        /// </summary>
        /// <param name="connName">数据库连接名称</param>
        /// <returns>连接对象</returns>
        protected ReferenceConnection GetRefConnectionWithoutTx(string connName)
        {
            ReferenceConnection refConnection = null;

            GraphWithoutTransaction connections = this.GraphWithoutTx;

            lock (connections)
            {
                if (connections.TryGetValue(connName, out refConnection) == false)
                {
                    DbConnection connection = DbConnectionManager.GetConnection(connName);
                    this._isConnectionCreator = true;

                    refConnection = new ReferenceConnection(connName, connection);
                    connections.Add(connName, refConnection);
                }
                else
                {
                    refConnection.ReferenceCount++;
                }
            }

            return(refConnection);
        }