Beispiel #1
0
        /// <summary>
        /// Change server in connection string
        /// </summary>
        /// <param name="pConnectionName">Existing connection string in app.config</param>
        /// <param name="pServerName">Replace server name currently in app.config with this server name</param>
        public bool ChangeServer(string pConnectionName, string pServerName)
        {
            try
            {
                // Assert named connection string pConnectionName exists
                if (ConnectionStrings.Cast <ConnectionStringSettings>().FirstOrDefault((css) => css.Name == pConnectionName) != null)
                {
                    Configuration configuration = OpenExeConfiguration(_assemblyHelpers.CallingNamespace());
                    var           section       = (ConnectionStringsSection)configuration.GetSection("connectionStrings");

                    var currentConnectionString = section.ConnectionStrings[pConnectionName].ConnectionString;

                    var builder = new SqlConnectionStringBuilder
                    {
                        ConnectionString = currentConnectionString,
                        DataSource       = pServerName
                    };

                    section.ConnectionStrings[pConnectionName].ConnectionString = builder.ConnectionString;
                    configuration.Save(ConfigurationSaveMode.Modified);
                    RefreshSection("connectionStrings");
                }

                return(true);
            }
            catch (Exception)
            {
                return(false);
            }
        }
Beispiel #2
0
        /// <summary>
        /// Change initial catalog for specific connection string
        /// </summary>
        /// <param name="pConnectionName">Existing connection string in app.config</param>
        /// <param name="pCatalog">Replace current catalog with replacement catalog</param>
        public void ChangeInitialCatalog(string pConnectionName, string pCatalog)
        {
            // Assert named connection string pConnectionName exists
            if (ConnectionStrings.Cast <ConnectionStringSettings>().FirstOrDefault((css) => css.Name == pConnectionName) != null)
            {
                Configuration configuration = OpenExeConfiguration(_assemblyHelpers.CallingNamespace());
                var           section       = (ConnectionStringsSection)configuration.GetSection("connectionStrings");

                var currentConnectionString = section.ConnectionStrings[pConnectionName].ConnectionString;

                var builder = new SqlConnectionStringBuilder
                {
                    ConnectionString = currentConnectionString,
                    InitialCatalog   = pCatalog
                };

                section.ConnectionStrings[pConnectionName].ConnectionString = builder.ConnectionString;
                configuration.Save(ConfigurationSaveMode.Modified);
                RefreshSection("connectionStrings");
            }
        }
Beispiel #3
0
        /// <summary>
        /// Get properties for a single connection string
        /// </summary>
        /// <param name="pConnectionName"></param>
        /// <returns></returns>
        public ConnectionProperties Properties(string pConnectionName)
        {
            var connectionProperties = new ConnectionProperties();

            if (ConnectionStrings.Cast <ConnectionStringSettings>().FirstOrDefault((css) => css.Name == pConnectionName) == null)
            {
                return(connectionProperties);
            }

            Configuration configuration = OpenExeConfiguration(_assemblyHelpers.CallingNamespace());
            var           section       = (ConnectionStringsSection)configuration.GetSection("connectionStrings");

            try
            {
                var currentConnectionString = section.ConnectionStrings[pConnectionName].ConnectionString;

                var builder = new SqlConnectionStringBuilder
                {
                    ConnectionString = currentConnectionString
                };

                connectionProperties = new ConnectionProperties
                {
                    DataSource         = builder.DataSource,
                    InitialCatalog     = builder.InitialCatalog,
                    Authentication     = builder.Authentication,
                    AttachDbFilename   = builder.AttachDBFilename,
                    IntegratedSecurity = builder.IntegratedSecurity,
                    IsValid            = true
                };
            }
            catch (Exception)
            {
                connectionProperties.IsValid = false;
            }

            return(connectionProperties);
        }