Esempio n. 1
0
        /// <summary>
        /// 尝试从当前上下文中获取已存在的ConnectionScope,
        /// 如果没有已存在的实例,就用默认的连接字符串创建一个ConnectionScope实例
        /// </summary>
        /// <returns></returns>
        public static ConnectionScope GetExistOrCreate()
        {
            ConnectionScope current = s_current;

            if (current != null)
            {
                current._refCount++;                        // 增加引用计数
                return(current);
            }

            return(Create());
        }
Esempio n. 2
0
        /// <summary>
        /// 通过参数化SQL、SqlParameter数组的方式,创建CPQuery实例
        /// </summary>
        /// <param name="parameterizedSQL">参数化的SQL字符串</param>
        /// <param name="parameters">SqlParameter参数数组</param>
        /// <returns>CPQuery对象实例</returns>
        public static CPQuery Create(string parameterizedSQL, params DbParameter[] parameters)
        {
            if (string.IsNullOrEmpty(parameterizedSQL))
            {
                throw new ArgumentNullException("parameterizedSQL");
            }

            CPQuery query = new CPQuery(ConnectionScope.GetDefaultDbConext());

            query.Init(parameterizedSQL, parameters);
            return(query);
        }
Esempio n. 3
0
        /// <summary>
        /// 实现IDisposable接口
        /// </summary>
        public void Dispose()
        {
            _refCount--;

            if (_refCount == 0)
            {
                // 恢复之前的【当前】实例
                s_current = _lastInstance;

                if (this.Context != null)
                {
                    this.Context.Dispose();
                    this.Context = null;
                }
            }
        }
Esempio n. 4
0
        internal static DbContext GetDefaultDbConext()
        {
            //return ConnectionScope.Current.Context;

            ConnectionScope current = s_current;

            if (current == null)
            {
                if (Initializer.Instance.IsAutoCreateOneOffDbContext)
                {
                    DbContext context = DbContext.Create();
                    context.AutoDisposable = true;
                    return(context);
                }
                else
                {
                    throw new InvalidProgramException("当前代码执行环境中没有创建ConnectionScope的实例");
                }
            }
            else
            {
                return(current.Context);
            }
        }