Nisi ea exercitation magna id ut ut anim labore ex cupidatat ut quis nulla commodo sunt ullamco tempor aliqua, in cillum velit et ad amet. Sunt ut nostrud adipisicing elit, duis dolore cillum est in consectetur nisi eiusmod excepteur laboris tempor occaecat id dolor qui culpa irure ut deserunt lorem
Yeah, this will be replaced... But please enjoy the search!
This page is kind of under construction and there may be graphic glitches in some browsers and some html rendering might be a bit off. It'll get better.
Howto programmatically add, update and remove ConnectionStrings in Umbraco
I found that amazing no-one ever posted snippets to howto actually manipulate the web.config file when using Umbraco.
Anyway, lets keep it short.
Dont forget to import the businesslogic.dll into your project and reference it
usingSystem;usingSystem.Configuration;usingumbraco;// from businesslogic.dll
Adding a connectionString in web.config
Note, you should either replace the Camelot.SharePointProvider with your preferred provider or add a parameter to the method to be able to modify this dynamically.
/// <summary>/// Adds a new connectionString in web.config./// </summary>/// <param name="name">Name the connection</param>/// <param name="connString">The actual connectionstring. Read more at http://docs.bendsoft.com/camelot-net-connector/</param>publicstaticvoid AddConnection(string name, string connString){
var webConfig =new ExeConfigurationFileMap { ExeConfigFilename = GlobalSettings.FullpathToRoot+"web.config"};
var config = ConfigurationManager.OpenMappedExeConfiguration(webConfig, ConfigurationUserLevel.None);
config.ConnectionStrings.ConnectionStrings.Add(new ConnectionStringSettings(name, connString, "Camelot.SharePointProvider"));
config.Save();
ConfigurationManager.RefreshSection("connectionStrings");}
Updating a connectionString in web.config
Note, if the connectionString doesn't exist this method will create it for you
/// <summary>/// Update an existing connectionString, if it dosn't exist it will be created/// </summary>/// <param name="name">Name the connection</param>/// <param name="connString">The actual connectionstring. Read more at http://docs.bendsoft.com/camelot-net-connector/</param>publicstaticvoid UpdateConnection(string name, string connString){
var webConfig =new ExeConfigurationFileMap { ExeConfigFilename = GlobalSettings.FullpathToRoot+"web.config"};
var config = ConfigurationManager.OpenMappedExeConfiguration(webConfig, ConfigurationUserLevel.None);
var section =(ConnectionStringsSection)config.GetSection("connectionStrings");if(section.ConnectionStrings[name]!=null){// Update the connectionstring if it exist
section.ConnectionStrings[name].ConnectionString= connString;
config.Save();
ConfigurationManager.RefreshSection("connectionStrings");}else{// ...otherwise we add a new
AddConnection(name, connString);}}
Delete a connectionString in web.config
/// <summary>/// Remove a connectionString/// </summary>/// <param name="name">The name of the connectionString to remove</param>publicstaticvoid RemoveConnection(string name){
var webConfig =new ExeConfigurationFileMap { ExeConfigFilename = GlobalSettings.FullpathToRoot+"web.config"};
var config = ConfigurationManager.OpenMappedExeConfiguration(webConfig, ConfigurationUserLevel.None);
var section =(ConnectionStringsSection)config.GetSection("connectionStrings");// Die if the connectionstring dosn't existif(section.ConnectionStrings[name]==null) return;
var keys = section.ConnectionStrings;
keys.Remove(name);
config.Save();
ConfigurationManager.RefreshSection("connectionStrings");}
Check if connectionstring exist
/// <summary>/// Check if a connectionString with the same name already exist/// </summary>/// <param name="name">Name of the connection</param>/// <returns>True on existence, otherwise false</returns>privatestaticbool ConnectionExist(string name){
var webConfig =new ExeConfigurationFileMap { ExeConfigFilename = GlobalSettings.FullpathToRoot+"web.config"};
var config = ConfigurationManager.OpenMappedExeConfiguration(webConfig, ConfigurationUserLevel.None);
var section =(ConnectionStringsSection)config.GetSection("connectionStrings");// Return false if it doesn't exist, true if it doesreturn section.ConnectionStrings[name]!= null;}