Example #1
0
        public static bool Apply(
            this SqlTransaction transaction, string TRANSACTION_NAME, MessageString errorMsg)
        {
            bool result = false;

            try
            {
                transaction.Commit();
                result = true;
            }
            catch (Exception ex)
            {
                errorMsg.Append(ex.ToString());
                try
                {
                    if (SString.IsNotNone(TRANSACTION_NAME))
                    {
                        transaction.Rollback(TRANSACTION_NAME);
                    }
                    else
                    {
                        transaction.Rollback();
                    }
                }
                catch (Exception)
                {
                    errorMsg.Append(ex.ToString());
                }
            }
            return(result);
        }
    public static MessageString operator +(MessageString ms1, MessageString ms2)
    {
        MessageString ms = new MessageString(ms1.Length + ms2.Length);

        ms.Append(ms1.ToString());
        ms.Append(ms2.ToString());
        return(ms);
    }
        public static bool ApplyTransaction(
            this SqlConnection connection, MessageString transactionError,
            Func <SqlTransaction, MessageString, bool> myMethodCall)
        {
            string TRANSACTION_NAME = Guid.NewGuid().ToString().Substring(
                0, CstVars.MAX_SQL_TRANSACTION_NAME_LENGTH
                );

            using (SqlTransaction transaction = connection.BeginTransaction(TRANSACTION_NAME))
            {
                if (myMethodCall(transaction, transactionError).IsTrue())
                {
                    return(SSqlTransaction.Apply(transaction, TRANSACTION_NAME, transactionError));
                }

                try
                {
                    transaction.Rollback(TRANSACTION_NAME);
                }
                catch (Exception ex)
                {
                    transactionError.Append(ex.ToString());
                }
            }

            return(false);
        }
        public static IEnumerable <T> GetObjList <T>(this SqlDataReader dataReader, MessageString errorMsg)
            where T : ITableModel, new()
        {
            if (dataReader.IsNull())
            {
                return(null);
            }

            ICollection <T> objList = null;

            using (dataReader)
            {
                try
                {
                    objList = new List <T>(40);
                    while (dataReader.Read())
                    {
                        T obj = new T();
                        obj.Fill(dataReader);
                        objList.Add(obj);
                    }
                }
                catch (Exception ex) { errorMsg.Append(ex.ToString()); }
            }
            return(objList);
        }
 public static bool IsOpened(this SqlConnection connection, MessageString errorMsg)
 {
     if ((connection.State == ConnectionState.Open).IsNotTrue())
     {
         errorMsg.Append("Database connection object is not opened");
         return(false);
     }
     return(true);
 }
 public static bool ExIsOpen(this SqlConnection connection, MessageString errorMsg)
 {
     if (connection == null)
     {
         return(false);
     }
     if (connection.State != ConnectionState.Open)
     {
         try
         {
             connection.Open();
         }
         catch (Exception ex) { errorMsg.Append(ex.ToString()); }
     }
     return(true);
 }
        public static int Exec(
            this SqlConnection connection, SqlTransaction transaction, CommandType commandType,
            string sqlText, IEnumerable <SqlParameter> parmList, MessageString errorMsg)
        {
            if (IsOpened(connection, errorMsg).IsNotTrue())
            {
                return(-1);
            }

            try
            {
                return(Exec(connection, transaction, commandType, sqlText, parmList));
            }
            catch (Exception ex) { errorMsg.Append(ex.ToString()); }

            return(-1);
        }
 public static bool ExIsReady(this SqlConnection connction, MessageString errorMsg)
 {
     if (ExIsOpen(connction, errorMsg) == false)
     {
         return(false);
     }
     try
     {
         using (SqlCommand command = new SqlCommand("select 1", connction))
             using (SqlDataReader reader = command.ExecuteReader())
                 if (reader.Read())
                 {
                     return(true);
                 }
     }
     catch (Exception ex) { errorMsg.Append(ex.ToString()); }
     return(false);
 }
 public static bool DaIsReady(this IDbConnection connction, MessageString errorMsg = null)
 {
     if (connction.DaIsOpen(errorMsg) == false)
     {
         return(false);
     }
     try
     {
         using (var command = connction.CreateCommand())
         {
             command.CommandText = _IS_READY_COMMAND_TEXT;
             command.Connection  = connction;
             return(((int)command.ExecuteScalar()) == 1);
         }
     }
     catch (Exception ex) { errorMsg?.Append(ex.ToString()); }
     return(false);
 }
        public static bool DaIsOpen(this IDbConnection connection, MessageString errorMsg = null)
        {
            if (connection == null)
            {
                return(false);
            }
            if (connection.State == ConnectionState.Open)
            {
                return(true);
            }

            try
            {
                connection.Open();
                return(true);
            }
            catch (Exception ex) { errorMsg?.Append(ex.ToString()); }
            return(false);
        }
        public static SqlDataReader Get(
            this SqlConnection connection, string sqlText, CommandType commandType,
            CommandBehavior commandBehavior, IEnumerable <SqlParameter> parmList, MessageString errorMsg)
        {
            if (IsOpened(connection, errorMsg).IsNotTrue())
            {
                return(null);
            }

            try
            {
                return(Get(connection, sqlText, commandType, commandBehavior, parmList));
            }
            catch (Exception ex)
            {
                errorMsg.Append(ex.ToString());
            }

            return(null);
        }