Example #1
0
        public object ExecuteScalar(QueryCommand qry)
        {
            WriteToLog(() => string.Format("ExecuteScalar(QueryCommand): {0}.", qry.CommandSql));

            object result;

            using (AutomaticConnectionScope automaticConnectionScope = new AutomaticConnectionScope(this))
            {
                DbCommand cmd = Factory.CreateCommand();
                cmd.Connection = automaticConnectionScope.Connection;
                if (automaticConnectionScope.IsUsingSharedConnection && CurrentSharedTransaction != null)
                {
                    cmd.Transaction = CurrentSharedTransaction;
                }
                cmd.CommandType = qry.CommandType;
                cmd.CommandText = qry.CommandSql;
                AddParams(cmd, qry);
                result = cmd.ExecuteScalar();
            }

            return(result);
        }
Example #2
0
        public DbDataReader ExecuteReader(QueryCommand qry)
        {
            AutomaticConnectionScope scope = new AutomaticConnectionScope(this);

            WriteToLog(() => string.Format("ExecuteReader(QueryCommand):\r\n{0}", qry.CommandSql));

            DbCommand cmd = scope.Connection.CreateCommand();

            cmd.Connection = scope.Connection; //CreateConnection();
            if (scope.IsUsingSharedConnection && CurrentSharedTransaction != null)
            {
                cmd.Transaction = CurrentSharedTransaction;
            }
            cmd.CommandText = qry.CommandSql;
            cmd.CommandType = qry.CommandType;

            AddParams(cmd, qry);

            //this may look completely lame
            //but there is a bug in here...

            DbDataReader rdr;

            //Thanks jcoenen!
            try
            {
                // if it is a shared connection, we shouldn't be telling the reader to close it when it is done
                rdr = scope.IsUsingSharedConnection ? cmd.ExecuteReader() : cmd.ExecuteReader(CommandBehavior.CloseConnection);
            }
            catch (Exception)
            {
                // AutoConnectionScope will figure out what to do with the connection
                scope.Dispose();
                //rethrow retaining stack trace.
                throw;
            }

            return(rdr);
        }
Example #3
0
        public int ExecuteQuery(QueryCommand qry)
        {
            WriteToLog(() => string.Format("ExecuteQuery(QueryCommand): {0}.", qry.CommandSql));

            int result;

            using (AutomaticConnectionScope automaticConnectionScope = new AutomaticConnectionScope(this))
            {
                DbCommand cmd = automaticConnectionScope.Connection.CreateCommand();
                if (automaticConnectionScope.IsUsingSharedConnection && CurrentSharedTransaction != null)
                {
                    cmd.Transaction = CurrentSharedTransaction;
                }
                cmd.CommandText = qry.CommandSql;
                cmd.CommandType = qry.CommandType;
                AddParams(cmd, qry);
                result = cmd.ExecuteNonQuery();
                // Issue 11 fix introduced by [email protected]
                qry.GetOutputParameters(cmd);
            }

            return(result);
        }