Example #1
0
        /// <summary>
        /// Get param value from connectionstring section
        /// </summary>
        /// <param name="paramName">Connection name</param>
        /// <param name="informationType">Connection information type to return</param>
        /// <returns>Value of indicated type from selected connection string on connectionstring node</returns>
        public static string GetFromConnectionString(string paramName, ConnectionStringInformationType informationType)
        {
            try
            {
                var connection = WebConfigurationManager.ConnectionStrings[paramName];
                if (connection == null)
                {
                    return(null);
                }

                switch (informationType)
                {
                case ConnectionStringInformationType.ConnectionString:
                    return(connection.ConnectionString);

                case ConnectionStringInformationType.Name:
                    return(connection.Name);

                case ConnectionStringInformationType.ProviderName:
                    return(connection.ProviderName);
                }

                return(null);
            }
            catch (Exception e)
            {
                Debug.WriteLine(e);
                return(null);
            }
        }
Example #2
0
        /// <summary>
        /// Modify a connection string value on connectionstring section
        /// </summary>
        /// <param name="paramName">Connection name</param>
        /// <param name="informationType">Connection information type to modify value</param>
        /// <param name="paramValue">New value for information type on connection string</param>
        /// <returns>Operation result</returns>
        public static bool SetToConnectionString(string paramName, ConnectionStringInformationType informationType, string paramValue)
        {
            try
            {
                var configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                if (configuration.ConnectionStrings == null)
                {
                    return(false);
                }

                if (configuration.ConnectionStrings.ConnectionStrings[paramName] == null)
                {
                    return(false);
                }

                switch (informationType)
                {
                case ConnectionStringInformationType.ConnectionString:
                    configuration.ConnectionStrings.ConnectionStrings[paramName].ConnectionString = paramValue;
                    break;

                case ConnectionStringInformationType.Name:
                    configuration.ConnectionStrings.ConnectionStrings[paramName].Name = paramValue;
                    break;

                case ConnectionStringInformationType.ProviderName:
                    configuration.ConnectionStrings.ConnectionStrings[paramName].ProviderName = paramValue;
                    break;
                }

                configuration.Save();
                ConfigurationManager.RefreshSection("connectionStrings");

                return(true);
            }
            catch (Exception e)
            {
                Debug.WriteLine(e);
                return(false);
            }
        }