Esempio n. 1
0
        /// <summary>
        /// Set data into the db
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="data_type"></param>
        public static void Set(Enum_Settings_Key key, object value, Enum_Settings_DataType data_type = Enum_Settings_DataType.Raw)
        {
            var Db = ModelBase.ServiceAppHost.TryResolve <IDbConnection>();

            if (Db.State != ConnectionState.Open)
            {
                Db = ModelBase.ServiceAppHost.TryResolve <IDbConnectionFactory>().Open();
            }


            // try to get it first
            var key_name = key.ToString();
            var item     = Db.Select <Settings>(x => x.Where(m => m.Key == key_name).Limit(1)).FirstOrDefault();

            if (item == null)
            {
                // insert new
                item = new Settings()
                {
                    Key = key_name, Value = value.ToString()
                };
                Db.Insert <Settings>(item);
            }
            else
            {
                // update
                item.Value = value.ToString();
                Db.Update <Settings>(item);
            }
            // we dispose the connection to save resource
            Db.Close();
        }
Esempio n. 2
0
        /// <summary>
        /// Get settings from Database. Use this function instead of the one in Settings class to avoid the confiction
        /// </summary>
        /// <param name="key"></param>
        /// <param name="default_value"></param>
        /// <param name="data_type"></param>
        /// <returns></returns>
        object Db_GetSetting(Enum_Settings_Key ekey, object default_value = null, Enum_Settings_DataType data_type = Enum_Settings_DataType.Raw)
        {
            string value = "";
            string key   = ekey.ToString();

            try
            {
                var k = Db.Select <Settings>(x => x.Where(m => m.Key == key).Limit(1)).FirstOrDefault();
                if (k == null)
                {
                    value = default_value.ToString();
                }
                else
                {
                    value = k.Value;
                }

                // // we dispose the connection to save resource
                Db.Close();

                switch (data_type)
                {
                case Enum_Settings_DataType.Int:
                    return(int.Parse(value.ToString()));

                case Enum_Settings_DataType.Double:
                    return(double.Parse(value.ToString()));

                case Enum_Settings_DataType.Percent:
                    value = value.Substring(0, value.Length - 1);
                    return(double.Parse(value));

                case Enum_Settings_DataType.String:
                    return(value.ToString());

                case Enum_Settings_DataType.DateTime:
                    return(DateTime.Parse(value.ToString()));

                case Enum_Settings_DataType.Raw:
                    return(k);

                default:
                    return(value);
                }
            }
            catch (Exception ex)
            {
                return(default_value);
            }
        }
Esempio n. 3
0
 public static object Get(Enum_Settings_Key key, object default_value = null, Enum_Settings_DataType data_type = Enum_Settings_DataType.Raw)
 {
     return(Get(key.ToString(), default_value, data_type));
 }