コード例 #1
0
ファイル: DbAreaRule.cs プロジェクト: buweixiaomi/DydCert
        /// <summary>
        /// 根据模板替换连接字符串
        /// </summary>
        /// <param name="connectstringTemplate"></param>
        /// <param name="o"></param>
        /// <returns></returns>
        public static string ReplaceConnectStringTemplate(string connectstringTemplate, DbPartitionConfig.Model.tb_dbpartition_config_model o)
        {
            if (o == null)
            {
                throw new Exception("找不到数据库连接分库配置");
            }

            return(connectstringTemplate.Replace("{partitionno}", o.partitionno + "").Replace("{dbserver}", o.dbserver).Replace("{dbname}", o.dbname)
                   .Replace("{dbuser}", o.dbuser).Replace("{dbpass}", o.dbpass));
        }
コード例 #2
0
        //static DbPartitionConfigHelper()
        //{
        //    try
        //    {
        //        //注册缓存刷新回调和初始化缓存
        //        var data = RefreashDbPartitionConfigCache();
        //        bool r = Cache.CacheFileProvider.Register(cachekey, new Cache.CacheFileInfo { IintervalTime = 1000 * 5, CacheData = data, RefreashMethod = DbPartitionConfigHelper.RefreashDbPartitionConfigCache });
        //        if (r == false)
        //        {
        //            ErrorLog.Write("注册db分区配置缓存失败", null);
        //        }
        //    }
        //    catch (Exception exp)
        //    {
        //        ErrorLog.Write("注册db分区配置出错", exp);
        //    }
        //}
        /// <summary>
        /// 刷新数据从数据库
        /// </summary>
        /// <returns></returns>
        private static List<tb_dbpartition_config_model> RefreshData(DateTime lastRefeshTime)
        {
            List<Model.tb_dbpartition_config_model> tbDbpartitions = new List<tb_dbpartition_config_model>();
            var isEnabledDepotsConnectByConfig = XXF.Common.XXFConfig.IsEnabledDepotsConnectByConfig;
            if (string.IsNullOrWhiteSpace(isEnabledDepotsConnectByConfig) ||
                isEnabledDepotsConnectByConfig.ToLower() == "false")
            {
                using (var conn = Db.DbConn.CreateConn(DbType.SQLSERVER, XXF.Common.XXFConfig.ConfigConnectString))
                {
                    conn.Open();
                    tbDbpartitions = new Dal.tb_dbpartition_config_dal().Get(conn, lastRefeshTime);
                }
            }
            else
            {
                var alldepotsconfig = System.Configuration.ConfigurationManager.AppSettings.AllKeys.Where(o => o.Contains("DepotsConnect")).ToList();

                foreach (var variable in alldepotsconfig)
                {
                    tb_dbpartition_config_model tbDbpartition = new tb_dbpartition_config_model();

                    var thisvale = System.Configuration.ConfigurationManager.AppSettings[variable];
                    tbDbpartition.partitionno = LibConvert.ObjToInt(variable.Split('_')[1]);
                    tbDbpartition.partitiontype = tbDbpartition.partitionno == 1 ? (byte)1 : (byte)2;
                    var arrys = thisvale.Split(';');
                    foreach (var arry in arrys)
                    {
                        var eachs = arry.Split('=');
                        switch (eachs[0])
                        {
                            case "server":
                                tbDbpartition.dbserver = eachs[1]; break;
                            case "Initial Catalog":
                                tbDbpartition.dbname = eachs[1]; break;
                            case "User ID":
                                tbDbpartition.dbuser = eachs[1]; break;
                            case "Password":
                                tbDbpartition.dbpass = eachs[1]; break;
                        }
                    }
                    tbDbpartitions.Add(tbDbpartition);
                }
            }
            if (configData == null)
            {
                configData = new Dictionary<string, tb_dbpartition_config_model>();
            }
            foreach (var m in tbDbpartitions)
            {
                string dicKey = GetDicKey(m.id.ToString());
                if (m.isDel)
                {
                    configData.Remove(dicKey);
                }
                else
                {
                    if (configData.ContainsKey(dicKey))
                    {
                        configData[dicKey] = m;
                    }
                    else
                    {
                        configData.Add(dicKey, m);
                    }
                }
            }

            List<tb_dbpartition_config_model> data = new List<tb_dbpartition_config_model>(configData.Values);
            return data;
        }