Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); ConfigurationSection connectionConfigurationSection = config.Sections["connectionStrings"]; ConnectionStringsSection connectionStringsSection = connectionConfigurationSection as ConnectionStringsSection; connectionStringsSection.ConnectionStrings["dataSource"].ConnectionString = ConStr; config.Save(); ConfigurationManager.RefreshSection("connectionStrings"); if (DataKey == null) { db = DatabaseFactory.CreateDatabase(); } else { db = DatabaseFactory.CreateDatabase(DataKey); }