Tag: vs2005
Connectionstrings.
by mysticslayer 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.
-
/// Add a connection string to the connection
-
/// strings section and store it in the
-
/// configuration file.
-
/// </summary>
-
/// <param name="csName">The name of the property.</param>
-
/// <param name="connectionString">The connectionstring as specified.</param>
-
public static void AddConnectionStrings(string csName, string connectionString)
-
{
-
-
// Get the configuration file.
-
System.Configuration.Configuration config =
-
ConfigurationManager.OpenExeConfiguration(
-
ConfigurationUserLevel.None);
-
-
// Add the connection string.
-
ConnectionStringsSection csSection = config.ConnectionStrings;
-
csSection.ConnectionStrings.Add(
-
new ConnectionStringSettings(csName,
-
connectionString, "System.Data.SqlClient"));
-
-
// Save the configuration file.
-
config.Save(ConfigurationSaveMode.Full);
-
}
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”>
-
/// First remove the old connectionstring and after that
-
/// add a connection string to the connectionstrings
-
/// section and store it in the configuration file.
-
/// </summary>
-
/// <param name="csName">The name of the property.</param>
-
/// <param name="connectionString">The connectionstring as specified.</param>
-
public static void UpdateConnectionStrings(string csName, string connectionString)
-
{
-
// Get the configuration file
-
System.Configuration.Configuration config =
-
ConfigurationManager.OpenExeConfiguration(
-
ConfigurationUserLevel.None);
-
-
// Remove the existing connectionstring.
-
config.ConnectionStrings.ConnectionStrings.Remove(csName);
-
// Add the connectionstring
-
ConnectionStringsSection csSection = config.ConnectionStrings;
-
csSection.ConnectionStrings.Add(
-
new ConnectionStringSettings(csName,
-
connectionString, "System.Data.SqlClient"));
-
-
// Save the configuration file
-
config.Save(ConfigurationSaveMode.Full);
-
}
-
</pre>