André Krijnen

Tag: vs2008


by on Apr.02, 2008, under Programming

Well I know that for VS 2005 or VS 2008 can maintain your web or app configuration file with alot of easy. Well adding shouldn’t be a harsh, but updating still is a somekind of problem. Allright, normally you have a static connectionstring for your (web)application. Well the company that I work for has an application that isn’t static at all.

Everything is dynamic, even the connectionstring. You know why? Well the application that someone made was used for multiple databases. And it is used for importing data. You can change the connectionstring by editing the web or app config. But that wasn’t an option. The people that use this application should change the connectionstring by the application itself.

Allright, the fellow that created this nice application wasn’t aware of the connectionstring configsection. So he didn’t used that part, but I said, well if you change a connectionstring, you can maintain it by ease with code. So I had to write two simple functions. Adding and updating of connectionstrings.

The first part of the code is adding the connectionstring. Well I used this code the Windows Application so I didn’t include any for web applications.

Well opening of the configuration file is used by the configurationmanager. The ConfigurationManager.OpenExeConfiguration is used for opening the app.config, with ConfigurationUserLevel none. So no user level is needed.

Then you take out the ConnectionString section.

/// <summary>
  1.         /// Add a connection string to the connection
  2.         /// strings section and store it in the
  3.         /// configuration file.
  4.         /// &lt;/summary&gt;
  5.         /// &lt;param name="csName"&gt;The name of the property.&lt;/param&gt;
  6.         /// &lt;param name="connectionString"&gt;The connectionstring as specified.&lt;/param&gt;
  7.         public static void AddConnectionStrings(string csName, string connectionString)
  8.         {
  10.             // Get the configuration file.
  11.             System.Configuration.Configuration config =
  12.                 ConfigurationManager.OpenExeConfiguration(
  13.                 ConfigurationUserLevel.None);
  15.             // Add the connection string.
  16.             ConnectionStringsSection csSection = config.ConnectionStrings;
  17.             csSection.ConnectionStrings.Add(
  18.                 new ConnectionStringSettings(csName,
  19.                     connectionString, "System.Data.SqlClient"));
  21.             // Save the configuration file.
  22.             config.Save(ConfigurationSaveMode.Full);
  23.         }

Well for updating it was hard to find any usefull code to update a connectionstring. It wasn’t possible to update a connectionstring, so I had the remove the connectionstring, and add a new one. Well isn’t that odd?? Microsoft left something undone… Well maybe it isn’t usefull to change your app.config, but well, I had to make it anyhow. So dynamic app.config or web.config files is little bit strange, because you can’t dynamically update it. No you have to remove it first by the configurationmanager.

<pre lang=”csharp”>

/// <summary>
  1.         /// First remove the old connectionstring and after that
  2.         /// add a connection string to the connectionstrings
  3.         /// section and store it in the configuration file.
  4.         /// &lt;/summary&gt;
  5.         /// &lt;param name="csName"&gt;The name of the property.&lt;/param&gt;
  6.         /// &lt;param name="connectionString"&gt;The connectionstring as specified.&lt;/param&gt;
  7.         public static void UpdateConnectionStrings(string csName, string connectionString)
  8.         {
  9.             // Get the configuration file
  10.             System.Configuration.Configuration config =
  11.                 ConfigurationManager.OpenExeConfiguration(
  12.                 ConfigurationUserLevel.None);
  14.             // Remove the existing connectionstring.
  15.             config.ConnectionStrings.ConnectionStrings.Remove(csName);
  16.             // Add the connectionstring
  17.             ConnectionStringsSection csSection = config.ConnectionStrings;
  18.             csSection.ConnectionStrings.Add(
  19.                 new ConnectionStringSettings(csName,
  20.                 connectionString, "System.Data.SqlClient"));
  22.             // Save the configuration file
  23.             config.Save(ConfigurationSaveMode.Full);
  24.         }
  25. &lt;/pre&gt;
Leave a Comment :, , , , , , , , more...

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!


A few highly recommended websites...