Beispiel #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="settingsKey"></param>
        /// <param name="valueJson"></param>
        /// <param name="count"></param>
        /// <param name="IntervalUpdateMilliseconds">更新的间隔时间(毫秒)</param>
        public void AddOrUpdate(SettingsKeyEnum settingsKey, string valueJson, int count)
        {
            string querySql = " SELECT * FROM  NY_Settings WHERE UserAccount=@UserAccount AND [Key]=@Key ";
            var    parmers  = new SqlParameter[] {
                new SqlParameter("UserAccount", userAccount),
                new SqlParameter("Key", settingsKey.ToString())
            };

            var isExsists = false;

            using (var reader = dbhelp.ExecuteReader(CommandType.Text, querySql, parmers))
            {
                if (reader.Read())
                {
                    isExsists = true;
                }
            }

            //表示存在,则更新数据
            if (isExsists)
            {
                var updateSql = String.Format(" UPDATE NY_Settings SET [Value]='{0}',Count={1},UpdateTime='{2}'  WHERE UserAccount=@UserAccount AND [Key]=@Key", valueJson, count, DateTime.Now.ToString());
                dbhelp.ExecuteNonQuery(CommandType.Text, updateSql, parmers);
            }
            else
            {
                var insertSql = String.Format(" INSERT INTO NY_Settings VALUES(@UserAccount,@Key,'{0}',{1},'{2}' ) ", valueJson, count, DateTime.Now.ToString());
                dbhelp.ExecuteNonQuery(CommandType.Text, insertSql, parmers);
            }
        }
Beispiel #2
0
        private DateTime?GetUpdateTime(SettingsKeyEnum settingsKey)
        {
            string querySql = " SELECT * FROM  NY_Settings WHERE UserAccount=@UserAccount AND [Key]=@Key ";
            var    parmers  = new SqlParameter[] {
                new SqlParameter("UserAccount", userAccount),
                new SqlParameter("Key", settingsKey.ToString())
            };

            var updateTime = String.Empty;

            using (var reader = dbhelp.ExecuteReader(CommandType.Text, querySql, parmers))
            {
                if (reader.Read())
                {
                    updateTime = reader["UpdateTime"].ToString();
                }
            }

            if (String.IsNullOrEmpty(updateTime))
            {
                return(null);
            }
            else
            {
                return(DateTime.Parse(updateTime));
            }
        }
Beispiel #3
0
        public string GetSettingsValue(SettingsKeyEnum settingsKey, out int count)
        {
            string value = String.Empty;

            count = 0;

            string querySql = " SELECT * FROM  NY_Settings WHERE UserAccount=@UserAccount AND [Key]=@Key ";
            var    parmers  = new SqlParameter[] {
                new SqlParameter("UserAccount", userAccount),
                new SqlParameter("Key", settingsKey.ToString())
            };

            var reader = dbhelp.ExecuteReader(CommandType.Text, querySql, parmers);

            if (reader.Read())
            {
                count = int.Parse(reader["Count"].ToString());
                value = reader["Value"].ToString();
            }

            return(value);
        }
Beispiel #4
0
        /// <summary>
        /// 通过更新间隔来判断当前数据是否需要更新
        /// 只有大于间隔时间才会更新
        /// </summary>
        /// <param name="settingsKey"></param>
        /// <param name="syncIntervalMillisecond"></param>
        /// <returns></returns>
        public bool IsAllowUpdate(SettingsKeyEnum settingsKey, int syncIntervalMillisecond)
        {
            if (syncIntervalMillisecond == 0)
            {
                //虽然是立即更新,但是为了防止快速刷新更新重复,设置为间隔2秒更新一次
                syncIntervalMillisecond = 2 * 1000;
            }

            var rs = true;

            var updateTime = GetUpdateTime(settingsKey);

            if (updateTime.HasValue)
            {
                if ((DateTime.Now - updateTime.Value).TotalMilliseconds < syncIntervalMillisecond)
                {
                    rs = false;
                }
            }

            return(rs);
        }