コード例 #1
0
        /// <summary>
        /// 加载所有链接字符串
        /// </summary>
        /// <param name="root">根节点</param>
        private static void LoadAllConnString(RegistryKey root)
        {
            if (root == null)
            {
                throw new MyException("注册表LocalMachine\\SOFTWARE\\mysoft节点下没有配置任何的连接信息");
            }

            foreach (var name in root.GetSubKeyNames())
            {
                // 读取子节点信息
                var node = GetSubKey(root, name);

                // 排除非连接字符串的配置项
                if (!node.GetValueNames().Contains("ServerName") ||
                    !node.GetValueNames().Contains("DBName") ||
                    !node.GetValueNames().Contains("UserName") ||
                    !node.GetValueNames().Contains("SaPassword"))
                {
                    continue;
                }

                var conn = new ConnString()
                {
                    ServerName = node.GetValue("ServerName").ToString(),
                    DBName     = node.GetValue("DBName").ToString(),
                    UserName   = node.GetValue("UserName").ToString(),
                    SaPassword = node.GetValue("SaPassword").ToString()
                };

                CSHashtable.Add(name, conn);
            }
        }
コード例 #2
0
        /// <summary>
        /// 获取指定注册表的key值
        /// </summary>
        /// <param name="root"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        private static ConnString GetSubKeyConn(RegistryKey root, string name)
        {
            // 读取子节点信息
            var node = GetSubKey(root, name);

            // 如果读取不到,则跳过
            if (node == null)
            {
                return(null);
            }

            // 如果为非连接字符串的配置项,则跳过
            if (!node.GetValueNames().Contains("ServerName") || !node.GetValueNames().Contains("DBName") ||
                !node.GetValueNames().Contains("UserName") || !node.GetValueNames().Contains("SaPassword"))
            {
                return(null);
            }

            var conn = new ConnString()
            {
                ServerName = node.GetValue("ServerName").ToString(),
                DBName     = node.GetValue("DBName").ToString(),
                UserName   = node.GetValue("UserName").ToString(),
                SaPassword = node.GetValue("SaPassword").ToString()
            };

            return(conn);
        }
コード例 #3
0
        /// <summary>
        /// 加载ASP.NET项目连接字符串
        /// </summary>
        /// <param name="root">根节点</param>
        private static void LoadASPNETConnString(RegistryKey root)
        {
            // 遍历web.config中配置过的连接字符串
            for (var i = 0; i < WebConfigurationManager.ConnectionStrings.Count; i++)
            {
                // 如果有提供者的名称,说明此配置是由数据库安装文件提供,不属于人为在web.config中配置的,所以直接跳过,避免出错。
                if (!string.IsNullOrEmpty(WebConfigurationManager.ConnectionStrings[i].ProviderName))
                {
                    continue;
                }

                // 如果连接字符串为空,则跳过。
                if (string.IsNullOrEmpty(WebConfigurationManager.ConnectionStrings[i].ConnectionString))
                {
                    continue;
                }

                // 如果存在相同的Key则直接抛异常
                if (CSHashtable.ContainsKey(WebConfigurationManager.ConnectionStrings[i].Name))
                {
                    throw new MyException("Web.Config中配置了相同Name的连接信息");
                }

                // 读取子节点信息
                var node = GetSubKey(root, WebConfigurationManager.ConnectionStrings[i].ConnectionString);

                // 如果读取不到,则跳过
                if (node == null)
                {
                    continue;
                }

                // 如果为非连接字符串的配置项,则跳过
                if (!node.GetValueNames().Contains("ServerName") || !node.GetValueNames().Contains("DBName") ||
                    !node.GetValueNames().Contains("UserName") || !node.GetValueNames().Contains("SaPassword"))
                {
                    continue;
                }

                var conn = new ConnString()
                {
                    ServerName = node.GetValue("ServerName").ToString(),
                    DBName     = node.GetValue("DBName").ToString(),
                    UserName   = node.GetValue("UserName").ToString(),
                    SaPassword = node.GetValue("SaPassword").ToString()
                };

                CSHashtable.Add(WebConfigurationManager.ConnectionStrings[i].Name, conn);
            }
        }