コード例 #1
0
        // <summary>
        // Attempts to locate a connection entry in the configuration based on the supplied context name.
        // </summary>
        // <param name="name"> The name to search for. </param>
        // <param name="config"> The configuration to search in. </param>
        // <returns> Connection string if found, otherwise null. </returns>
        private static ConnectionStringSettings FindConnectionInConfig(string name, AppConfig config)
        {
            // Build a list of candidate names that might be found in the app.config/web.config file.
            // The first entry is the full name.
            var candidates = new List <string>
            {
                name
            };

            // Second entry is full name with namespace stripped out.
            var lastDot = name.LastIndexOf('.');

            if (lastDot >= 0 &&
                lastDot + 1 < name.Length)
            {
                candidates.Add(name.Substring(lastDot + 1));
            }

            // Now go through each candidate.  As soon as we find one that matches, stop.
            var appConfigConnection = (from c in candidates
                                       where config.GetConnectionString(c) != null
                                       select config.GetConnectionString(c)).FirstOrDefault();

            return(appConfigConnection);
        }
コード例 #2
0
            public void GetConnectionString_from_Configuration()
            {
                var config = new AppConfig(
                    CreateEmptyConfig().AddConnectionString("AppConfigTest", "FromConfiguration"));

                var conn = config.GetConnectionString("AppConfigTest");

                Assert.Equal("FromConfiguration", conn.ConnectionString);
            }
コード例 #3
0
            public void GetConnectionString_from_ConnectionStringSettingsCollection()
            {
                var strings = new ConnectionStringSettingsCollection();

                strings.Add(new ConnectionStringSettings("AppConfigTest", "FromConnectionStringSettingsCollection", ""));
                var config = new AppConfig(strings);

                var conn = config.GetConnectionString("AppConfigTest");

                Assert.Equal("FromConnectionStringSettingsCollection", conn.ConnectionString);
            }
コード例 #4
0
        private static ConnectionStringSettings FindConnectionInConfig(
            string name,
            AppConfig config)
        {
            List <string> source = new List <string>()
            {
                name
            };
            int num = name.LastIndexOf('.');

            if (num >= 0 && num + 1 < name.Length)
            {
                source.Add(name.Substring(num + 1));
            }
            return(source.Where <string>((Func <string, bool>)(c => config.GetConnectionString(c) != null)).Select <string, ConnectionStringSettings>((Func <string, ConnectionStringSettings>)(c => config.GetConnectionString(c))).FirstOrDefault <ConnectionStringSettings>());
        }
コード例 #5
0
        // <summary>
        // Gets the connection information represented by this instance.
        // </summary>
        // <param name="config"> Configuration to use if connection comes from the configuration file. </param>
        internal ConnectionStringSettings GetConnectionString(AppConfig config)
        {
            DebugCheck.NotNull(config);

            if (_connectionName != null)
            {
                var result = config.GetConnectionString(_connectionName);
                if (result == null)
                {
                    throw Error.DbConnectionInfo_ConnectionStringNotFound(_connectionName);
                }

                return result;
            }

            return new ConnectionStringSettings(null, _connectionString, _providerInvariantName);
        }
コード例 #6
0
        /// <summary>
        ///     Gets the connection information represented by this instance.
        /// </summary>
        /// <param name = "config">Configuration to use if connection comes from the configuration file.</param>
        internal ConnectionStringSettings GetConnectionString(AppConfig config)
        {
            //Contract.Requires(config != null);

            if (_connectionName != null)
            {
                var result = config.GetConnectionString(_connectionName);
                if (result == null)
                {
                    throw Error.DbConnectionInfo_ConnectionStringNotFound(_connectionName);
                }

                return result;
            }

            return new ConnectionStringSettings(null, _connectionString, _providerInvariantName);
        }
コード例 #7
0
        /// <summary>
        ///     Attempts to locate a connection entry in the configuration based on the supplied context name.
        /// </summary>
        /// <param name="name"> The name to search for. </param>
        /// <param name="config"> The configuration to search in. </param>
        /// <returns> Connection string if found, otherwise null. </returns>
        private static ConnectionStringSettings FindConnectionInConfig(string name, AppConfig config)
        {
            // Build a list of candidate names that might be found in the app.config/web.config file.
            // The first entry is the full name.
            var candidates = new List<string>
                {
                    name
                };

            // Second entry is full name with namespace stripped out.
            var lastDot = name.LastIndexOf('.');
            if (lastDot >= 0
                && lastDot + 1 < name.Length)
            {
                candidates.Add(name.Substring(lastDot + 1));
            }

            // Now go through each candidate.  As soon as we find one that matches, stop.
            var appConfigConnection = (from c in candidates
                                       where config.GetConnectionString(c) != null
                                       select config.GetConnectionString(c)).FirstOrDefault();
            return appConfigConnection;
        }