Exemplo n.º 1
0
        /// <summary>
        /// 初始化DB连接对象
        /// </summary>
        /// <returns></returns>
        public static List <MutiDBOperate> MutiDBConnInit()
        {
            var path = "appsettings.json";

            List <MutiDBOperate> listdatabase = new List <MutiDBOperate>();

            // 从配置项文件 appsettings.json 中获取节点 DBS 配置的数据库连接信息
            using (var streamReader = new StreamReader(path))
            {
                using (var jsonReader = new JsonTextReader(streamReader))
                {
                    var jObj    = (JObject)JToken.ReadFrom(jsonReader);
                    var dbsNode = jObj["DBS"];
                    if (dbsNode != null)
                    {
                        for (int i = 0; i < dbsNode.Count(); i++)
                        {
                            var dbOperation = new MutiDBOperate()
                            {
                                Conn   = dbsNode[i]["Connection"].ObjToString(),
                                ConnId = dbsNode[i]["ConnId"].ObjToString(),
                                DbType = (DataBaseType)(dbsNode[i]["DBType"].ObjToInt()),
                            };

                            listdatabase.Add(SpecialDbString(dbOperation));
                        }
                    }
                }
                return(listdatabase);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// 如果系统有指定文件指定新的db连接方式,则采用文件的,否则采用代码的配置文件的
        /// </summary>
        /// <param name="mutiDBOperate"></param>
        /// <returns></returns>
        private static MutiDBOperate SpecialDbString(MutiDBOperate mutiDBOperate)
        {
            if (mutiDBOperate.DbType == DataBaseType.Sqlite)
            {
                mutiDBOperate.Conn = $"DataSource=" + Path.Combine(Environment.CurrentDirectory, mutiDBOperate.Conn);
            }
            else if (mutiDBOperate.DbType == DataBaseType.SqlServer)
            {
                mutiDBOperate.Conn = DifDBConnOfSecurity(@"D:\my-file\dbCountPsw1.txt", @"c:\my-file\dbCountPsw1.txt", mutiDBOperate.Conn);
            }
            else if (mutiDBOperate.DbType == DataBaseType.MySql)
            {
                mutiDBOperate.Conn = DifDBConnOfSecurity(@"D:\my-file\dbCountPsw1_MySqlConn.txt", @"c:\my-file\dbCountPsw1_MySqlConn.txt", mutiDBOperate.Conn);
            }
            else if (mutiDBOperate.DbType == DataBaseType.Oracle)
            {
                mutiDBOperate.Conn = DifDBConnOfSecurity(@"D:\my-file\dbCountPsw1_OracleConn.txt", @"c:\my-file\dbCountPsw1_OracleConn.txt", mutiDBOperate.Conn);
            }

            return(mutiDBOperate);
        }