Beispiel #1
0
 /// <summary>
 /// 执行command命令,并返回受影响行数
 /// </summary>
 /// <param name="command">command包含执行的命令</param>
 /// <returns></returns>
 public virtual int ExecuteNonQuery(DbCommand command)
 {
     using (DatabaseConnectionWrapper wrapper = GetOpenConnection())
     {
         PrepareCommand(command, wrapper.Connection);
         return(DoExecuteNonQuery(command));
     }
 }
Beispiel #2
0
 /// <summary>
 /// 执行command命令并返回一个IDataReader,通过它可以读取结果
 /// 调用者在调用完毕后,负责关闭IDataReader
 /// </summary>
 /// <param name="command"></param>
 /// <returns></returns>
 public virtual IDataReader ExecuteReader(DbCommand command)
 {
     using (DatabaseConnectionWrapper wrapper = GetOpenConnection())
     {
         PrepareCommand(command, wrapper.Connection);
         IDataReader realReader = DoExecuteReader(command, CommandBehavior.Default);
         return(CreateWrappedReader(wrapper, realReader));
     }
 }
Beispiel #3
0
        //public virtual IDataReader ExecuteReader(DbCommand command)
        //{
        //    using (DatabaseConnectionWrapper wrapper = GetOpenConnection())
        //    {
        //        PrepareCommand(command, wrapper.Connection);
        //        IDataReader realReader = DoExecuteReader(command, CommandBehavior.Default);

        //    }
        //}

        /// <summary>
        /// 执行command命令,并返回查询所返回的结果集中第一行的第一列。忽略额外的列或行
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        public virtual object ExecuteScalar(DbCommand command)
        {
            if (command == null)
            {
                throw new ArgumentNullException("command");
            }

            using (DatabaseConnectionWrapper wrapper = GetOpenConnection())
            {
                PrepareCommand(command, wrapper.Connection);
                return(DoExecuteScalar(command));
            }
        }
Beispiel #4
0
        public RefCountingDataReader(DatabaseConnectionWrapper connection, IDataReader innerReader)
            : base(innerReader)
        {
            if (connection == null)
            {
                throw new ArgumentNullException("connection");
            }
            if (innerReader == null)
            {
                throw new ArgumentNullException("innerReader");
            }

            connectionWrapper = connection;
            connectionWrapper.AddRef();
        }
Beispiel #5
0
        public static DatabaseConnectionWrapper GetConnection(Database db)
        {
            Transaction currentTransaction = Transaction.Current;

            if (currentTransaction == null)
            {
                return(null);
            }

            Dictionary <string, DatabaseConnectionWrapper> connectionList;
            DatabaseConnectionWrapper connection;

            lock (transactionConnections)
            {
                if (!transactionConnections.TryGetValue(currentTransaction, out connectionList))
                {
                    connectionList = new Dictionary <string, DatabaseConnectionWrapper>();
                    transactionConnections.Add(currentTransaction, connectionList);

                    currentTransaction.TransactionCompleted += OnTransactionCompleted;
                }
            }

            lock (connectionList)
            {
                if (!connectionList.TryGetValue(db.ConnectionString, out connection))
                {
                    DbConnection dbConnection = db.GetNewOpenConnection();
                    connection = new DatabaseConnectionWrapper(dbConnection);
                    connectionList.Add(db.ConnectionString, connection);
                }

                connection.AddRef();
            }

            return(connection);
        }
Beispiel #6
0
 /// <summary>
 /// 创建一个被包装的DataReader对象,用来控制连接对象connection
 /// </summary>
 /// <param name="connection">Connectinon + refCount</param>
 /// <param name="innerReader">被包装的reader</param>
 /// <returns></returns>
 protected virtual IDataReader CreateWrappedReader(DatabaseConnectionWrapper connection, IDataReader innerReader)
 {
     return(new RefCountingDataReader(connection, innerReader));
 }
Beispiel #7
0
        /// <summary>
        /// 获取一个被包装的connection
        /// 如果一个事务正在活动,这个connection对象将不会被disposed
        /// </summary>
        /// <returns></returns>
        protected DatabaseConnectionWrapper GetOpenConnection()
        {
            DatabaseConnectionWrapper connection = TransactionScopeConnections.GetConnection(this);

            return(connection ?? GetWrappedConnection());
        }