Example #1
0
 public static int ConditionExecuteNonQuery(Command JudgeCommand, IEnumerable <Command> DefaultCommands, IEnumerable <Command> SubCommands, Transaction trans)
 {
     Debug.Assert(JudgeCommand != null, "DbProvider.ConditionExecuteNonQuery: JudgeCommand Cannot be NULL");
     if (JudgeCommand == null)
     {
         return(-1);
     }
     try
     {
         if (JudgeCommand.ExecuteScalar(trans) != null && DefaultCommands != null)
         {
             return(Extension.ExecuteNonQuery(DefaultCommands, trans));
         }
         else if (SubCommands != null)
         {
             return(Extension.ExecuteNonQuery(SubCommands, trans));
         }
         else
         {
             return(-1);
         }
     }
     catch (Exception e)
     {
         #if DEBUG
         CommandException ce = new CommandException("ConditionExecuteNonQuery", JudgeCommand, e);
         Debug.WriteLine(ce);
         throw ce;
         #endif
     }
     return(-1);
 }
Example #2
0
 public static int ExecuteNonQuery(this IEnumerable<Command> cmds, Transaction trans)
 {
     Debug.Assert(cmds != null, "IEnumerable<Command>.ExecuteNonQuery: cmds Cannot be NULL");
     if (cmds == null)
         return -1;
     DbConnection conn = null;
     DbTransaction _trans = null;
     Command _cmd = null;
     try
     {
         if (trans != null)
         {
             conn = trans.CurrentConnection.OriConnection;
             _trans = trans.CurrentTransaction;
         }
         else
         {
             conn = DbProvider.Current.CreateConnection();
             conn.Open();
             _trans = conn.BeginTransaction();
         }
         int res = 0;
         foreach (Command cmd in cmds)
         {
             _cmd = cmd;
             res = cmd.AsCommonCommand(conn, _trans).ExecuteNonQuery();
         }
         if (trans == null)
             _trans.Commit();
         return res;
     }
     catch (Exception e)
     {
         #if DEBUG
         CommandException ce = new CommandException("ExecuteNonQuery", _cmd, e);
         Debug.WriteLine(ce);
         throw ce;
         #endif
         try
         {
             if (trans == null)
                 trans.Rollback();
             else
                 trans.success = false;
         }
         catch (Exception ex)
         {
             #if DEBUG
             CommandException ce2 = new CommandException("ExecuteNonQuery", _cmd, ex);
             Debug.WriteLine(ce2);
             throw ce2;
             #endif
         }
         return -1;
     }
     finally
     {
         try
         {
             if (trans == null)
                 conn.Close();
         }
         catch (Exception e)
         {
             #if DEBUG
             CommandException ce = new CommandException("ExecuteNonQuery", _cmd, e);
             Debug.WriteLine(ce);
             throw ce;
             #endif
         }
     }
 }
Example #3
0
        public Object ExecuteScalar(Transaction trans)
        {
            DbConnection conn = null;
            bool         err  = false;

            try
            {
                DbCommand cmd;
                if (trans != null)
                {
                    cmd = this.AsCommonCommand(trans.CurrentConnection.OriConnection, trans.CurrentTransaction);
                }
                else
                {
                    conn = DbProvider.Current.CreateConnection();
                    conn.Open();
                    cmd = this.AsCommonCommand(conn, null);
                }
                return(cmd.ExecuteScalar());
            }
            catch (Exception e)
            {
                err = true;
                if (trans != null)
                {
                    trans.success = false;
                }

                #if DEBUG
                CommandException ce = new CommandException("ExecuteScalar", this, e);
                Debug.WriteLine(ce);
                throw ce;
                #endif

                return(null);
            }
            finally
            {
                try
                {
                    if (trans == null)
                    {
                        conn.Close();
                    }
                }
                catch (Exception e)
                {
                    if (trans != null)
                    {
                        trans.success = false;
                    }

                    #if DEBUG
                    if (!err)
                    {
                        CommandException ce = new CommandException("ExecuteScalar", this, e);
                        Debug.WriteLine(ce);
                        throw ce;
                    }
                    #endif
                }
            }
        }
Example #4
0
        public bool ExecuteReader(DBReaderDelegate func, Transaction trans)
        {
            Debug.Assert(func != null, "DBReaderDelegate Cannot be NULL");
            DbConnection conn = null;
            DbDataReader r    = null;
            bool         err  = false;

            try
            {
                DbCommand cmd;
                if (trans != null)
                {
                    cmd = this.AsCommonCommand(trans.CurrentConnection.OriConnection, trans.CurrentTransaction);
                }
                else
                {
                    conn = DbProvider.Current.CreateConnection();
                    conn.Open();
                    cmd = this.AsCommonCommand(conn, null);
                }
                r = cmd.ExecuteReader();
                while (r.Read() && func(r))
                {
                    ;
                }
                return(true);
            }
            catch (Exception e)
            {
                err = true;
                if (trans != null)
                {
                    trans.success = false;
                }

                #if DEBUG
                CommandException ce = new CommandException("ExecuteReader", this, e);
                Debug.WriteLine(ce);
                throw ce;
                #endif

                return(false);
            }
            finally
            {
                try
                {
                    r.Close();
                }
                catch (Exception e)
                {
                    if (trans != null)
                    {
                        trans.success = false;
                    }

                    #if DEBUG
                    if (!err)
                    {
                        CommandException ce = new CommandException("ExecuteReader", this, e);
                        Debug.WriteLine(ce);
                        throw ce;
                    }
                    #endif
                }
                try
                {
                    if (trans == null)
                    {
                        conn.Close();
                    }
                }
                catch (Exception e)
                {
                    if (trans != null)
                    {
                        trans.success = false;
                    }

                    #if DEBUG
                    if (!err)
                    {
                        CommandException ce = new CommandException("ExecuteReader", this, e);
                        Debug.WriteLine(ce);
                        throw ce;
                    }
                    #endif
                }
            }
        }
Example #5
0
        public NameValueList[] Execute(Transaction trans)
        {
            DbConnection conn = null;
            DbDataReader r    = null;
            bool         err  = false;

            try
            {
                DbCommand cmd;
                if (trans != null)
                {
                    cmd = this.AsCommonCommand(trans.CurrentConnection.OriConnection, trans.CurrentTransaction);
                }
                else
                {
                    conn = DbProvider.Current.CreateConnection();
                    conn.Open();
                    cmd = this.AsCommonCommand(conn, null);
                }
                r = cmd.ExecuteReader();
                List <NameValueList> res  = new List <NameValueList>();
                List <string>        keys = new List <string>();
                while (r.Read())
                {
                    NameValueList data = new NameValueList();
                    for (int i = 0; i < r.FieldCount; i++)
                    {
                        if (res.Count == 0)
                        {
                            keys.Add(r.GetName(i));
                        }
                        data.Add(keys[i], r.GetValue(i));
                    }
                    res.Add(data);
                }
                r.Close();
                return(res.ToArray());
            }
            catch (Exception e)
            {
                err = true;
                if (trans != null)
                {
                    trans.success = false;
                }

                #if DEBUG
                CommandException ce = new CommandException("Execute", this, e);
                Debug.WriteLine(ce);
                throw ce;
                #endif

                return(new NameValueList[] { });
            }
            finally
            {
                try
                {
                    r.Close();
                }
                catch (Exception e)
                {
                    if (trans != null)
                    {
                        trans.success = false;
                    }

                    #if DEBUG
                    if (!err)
                    {
                        CommandException ce = new CommandException("Execute", this, e);
                        Debug.WriteLine(ce);
                        throw ce;
                    }
                    #endif
                }
                try
                {
                    if (trans == null)
                    {
                        conn.Close();
                    }
                }
                catch (Exception e)
                {
                    if (trans != null)
                    {
                        trans.success = false;
                    }

                    #if DEBUG
                    if (!err)
                    {
                        CommandException ce = new CommandException("Execute", this, e);
                        Debug.WriteLine(ce);
                        throw ce;
                    }
                    #endif
                }
            }
        }
Example #6
0
        public static int ExecuteNonQuery(this IEnumerable <Command> cmds, Transaction trans)
        {
            Debug.Assert(cmds != null, "IEnumerable<Command>.ExecuteNonQuery: cmds Cannot be NULL");
            if (cmds == null)
            {
                return(-1);
            }
            DbConnection  conn   = null;
            DbTransaction _trans = null;
            Command       _cmd   = null;

            try
            {
                if (trans != null)
                {
                    conn   = trans.CurrentConnection.OriConnection;
                    _trans = trans.CurrentTransaction;
                }
                else
                {
                    conn = DbProvider.Current.CreateConnection();
                    conn.Open();
                    _trans = conn.BeginTransaction();
                }
                int res = 0;
                foreach (Command cmd in cmds)
                {
                    _cmd = cmd;
                    res  = cmd.AsCommonCommand(conn, _trans).ExecuteNonQuery();
                }
                if (trans == null)
                {
                    _trans.Commit();
                }
                return(res);
            }
            catch (Exception e)
            {
                #if DEBUG
                CommandException ce = new CommandException("ExecuteNonQuery", _cmd, e);
                Debug.WriteLine(ce);
                throw ce;
                #endif
                try
                {
                    if (trans == null)
                    {
                        trans.Rollback();
                    }
                    else
                    {
                        trans.success = false;
                    }
                }
                catch (Exception ex)
                {
                    #if DEBUG
                    CommandException ce2 = new CommandException("ExecuteNonQuery", _cmd, ex);
                    Debug.WriteLine(ce2);
                    throw ce2;
                    #endif
                }
                return(-1);
            }
            finally
            {
                try
                {
                    if (trans == null)
                    {
                        conn.Close();
                    }
                }
                catch (Exception e)
                {
                    #if DEBUG
                    CommandException ce = new CommandException("ExecuteNonQuery", _cmd, e);
                    Debug.WriteLine(ce);
                    throw ce;
                    #endif
                }
            }
        }
Example #7
0
        public int ExecuteNonQuery(Transaction trans)
        {
            DbConnection conn = null;
            bool err = false;
            try
            {
                DbCommand cmd;
                if (trans != null)
                    cmd = this.AsCommonCommand(trans.CurrentConnection.OriConnection, trans.CurrentTransaction);
                else
                {
                    conn = DbProvider.Current.CreateConnection();
                    conn.Open();
                    cmd = this.AsCommonCommand(conn, null);
                }
                return cmd.ExecuteNonQuery();
            }
            catch (Exception e)
            {
                err = true;
                if (trans != null)
                    trans.success = false;

                #if DEBUG
                    CommandException ce = new CommandException("ExecuteNonQuery", this, e);
                    Debug.WriteLine(ce);
                    throw ce;
                #endif

                return -1;
            }
            finally
            {
                try
                {
                    if (trans == null)
                        conn.Close();
                }
                catch (Exception e)
                {
                    if (trans != null)
                        trans.success = false;

                    #if DEBUG
                        if (!err)
                        {
                            CommandException ce = new CommandException("ExecuteNonQuery", this, e);
                            Debug.WriteLine(ce);
                            throw ce;
                        }
                    #endif
                }
            }
        }
Example #8
0
        public bool ExecuteReader(DBReaderDelegate func, Transaction trans)
        {
            Debug.Assert(func != null, "DBReaderDelegate Cannot be NULL");
            DbConnection conn = null;
            DbDataReader r = null;
            bool err = false;
            try
            {
                DbCommand cmd;
                if (trans != null)
                    cmd = this.AsCommonCommand(trans.CurrentConnection.OriConnection, trans.CurrentTransaction);
                else
                {
                    conn = DbProvider.Current.CreateConnection();
                    conn.Open();
                    cmd = this.AsCommonCommand(conn, null);
                }
                r = cmd.ExecuteReader();
                while (r.Read() && func(r)) ;
                return true;
            }
            catch (Exception e)
            {
                err = true;
                if (trans != null)
                    trans.success = false;

                #if DEBUG
                    CommandException ce = new CommandException("ExecuteReader", this, e);
                    Debug.WriteLine(ce);
                    throw ce;
                #endif

                return false;
            }
            finally
            {
                try
                {
                    r.Close();
                }
                catch (Exception e)
                {
                    if (trans != null)
                        trans.success = false;

                    #if DEBUG
                        if (!err)
                        {
                            CommandException ce = new CommandException("ExecuteReader", this, e);
                            Debug.WriteLine(ce);
                            throw ce;
                        }
                    #endif
                }
                try
                {
                    if (trans == null)
                        conn.Close();
                }
                catch (Exception e)
                {
                    if (trans != null)
                        trans.success = false;

                    #if DEBUG
                        if (!err)
                        {
                            CommandException ce = new CommandException("ExecuteReader", this, e);
                            Debug.WriteLine(ce);
                            throw ce;
                        }
                    #endif
                }
            }
        }
Example #9
0
        public NameValueList[] Execute(Transaction trans)
        {
            DbConnection conn = null;
            DbDataReader r = null;
            bool err = false;
            try
            {
                DbCommand cmd;
                if (trans != null)
                    cmd = this.AsCommonCommand(trans.CurrentConnection.OriConnection, trans.CurrentTransaction);
                else
                {
                    conn = DbProvider.Current.CreateConnection();
                    conn.Open();
                    cmd = this.AsCommonCommand(conn, null);
                }
                r = cmd.ExecuteReader();
                List<NameValueList> res = new List<NameValueList>();
                List<string> keys = new List<string>();
                while (r.Read())
                {
                    NameValueList data = new NameValueList();
                    for (int i = 0; i < r.FieldCount; i++)
                    {
                        if (res.Count == 0)
                            keys.Add(r.GetName(i));
                        data.Add(keys[i], r.GetValue(i));
                    }
                    res.Add(data);
                }
                r.Close();
                return res.ToArray();
            }
            catch (Exception e)
            {
                err = true;
                if (trans != null)
                    trans.success = false;
                
                #if DEBUG
                    CommandException ce = new CommandException("Execute", this, e);
                    Debug.WriteLine(ce);
                    throw ce;
                #endif

                return new NameValueList[] { };
            }
            finally
            {
                try
                {
                    r.Close();
                }
                catch (Exception e)
                {
                    if (trans != null)
                        trans.success = false;

                    #if DEBUG
                        if (!err)
                        {
                            CommandException ce = new CommandException("Execute", this, e);
                            Debug.WriteLine(ce);
                            throw ce;
                        }
                    #endif
                }
                try
                {
                    if (trans == null)
                        conn.Close();
                }
                catch (Exception e)
                {
                    if (trans != null)
                        trans.success = false;

                    #if DEBUG
                        if (!err)
                        {
                            CommandException ce = new CommandException("Execute", this, e);
                            Debug.WriteLine(ce);
                            throw ce;
                        }
                    #endif
                }
            }
        }