Read / Write into App Settings and Connection String Section of App.Config using C#

10 Dec

Business Problem:

We have an application which connects to a database server for operations. Our company recently installed a backup database server for high availability. Now we want our application to connect to backup database server in-case one server goes down.

I have recently wrote code which first checks the availability of database servers and then it updates the App.Config to store the new available database server.

So one can use my code for reading and updating the “App.Config’s” App Settings, Connection Strings sections using C#.

Let me share code.

private bool IsServerConnected(string conString)
{
try
{
using (var l_oConnection = new SqlConnection(conString))
{
try
{
l_oConnection.Open();
return true;
}
catch (SqlException)
{
return false;
}
}
}
catch (SqlException exp)
{
labelOutPut.Text = “Invalid Connection String “;
return false;
}
}

/***********************************************************************/

private void updateAppSettingsSection()
{
/// <summary>
/// Update App Settings Section in App.Config File
/// </summary>
///
string connectionString1 = “Server=localhost;Database=OSL11NOV13;User Id=sa;Password=***;”;
string connectionString2 = “Server=localhost;Database=SecondDB;User Id=sa;Password=****;”;

if (IsServerConnected(connectionString1))
{

string appPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
string configFile = System.IO.Path.Combine(appPath, “App.config”);
ExeConfigurationFileMap configFileMap = new ExeConfigurationFileMap();
configFileMap.ExeConfigFilename = configFile;
System.Configuration.Configuration config = ConfigurationManager.OpenMappedExeConfiguration(configFileMap, ConfigurationUserLevel.None);

config.AppSettings.Settings[“connString”].Value = connectionString1;
config.Save();
ConfigurationManager.RefreshSection(“appSettings”);

labelOutPut.Text = “Connection String 1 is available and updated”;
}
else if (IsServerConnected(connectionString2))
{

string appPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
string configFile = System.IO.Path.Combine(appPath, “App.config”);
ExeConfigurationFileMap configFileMap = new ExeConfigurationFileMap();
configFileMap.ExeConfigFilename = configFile;
System.Configuration.Configuration config = ConfigurationManager.OpenMappedExeConfiguration(configFileMap, ConfigurationUserLevel.None);

/***************App Settings Section***********************/
config.AppSettings.Settings[“connString”].Value = connectionString2;

config.Save();

ConfigurationManager.RefreshSection(“appSettings”);

labelOutPut.Text = “Connection String 2 is available and updated”;
}
else
labelOutPut.Text = “None of Connection Strings are available and updated”;

}

/***********************************************************************/

private void readAppSettingsSection()
{

/// <summary>
/// Read App Settigs Section of App.Config File
/// </summary>
/// <param name=”sender”></param>
/// <param name=”e”></param>

string appPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
string configFile = System.IO.Path.Combine(appPath, “App.config”);
ExeConfigurationFileMap configFileMap = new ExeConfigurationFileMap();
configFileMap.ExeConfigFilename = configFile;
System.Configuration.Configuration config = ConfigurationManager.OpenMappedExeConfiguration(configFileMap, ConfigurationUserLevel.None);

string connTest = config.AppSettings.Settings[“connString”].Value;
labelAppSettings.Text = “App Settings Section: ” + connTest;
}

/***********************************************************************/

private void updateConnStringSection()
{

/// <summary>
/// Updating Connection Strings Section in App.config
/// </summary>
/// <param name=”sender”></param>
/// <param name=”e”></param>

string connectionString1 = “Server=localhost;Database=OSL11NOV13;User Id=sa;Password=****;”;
string connectionString2 = “Server=localhost;Database=SecondDB;User Id=sa;Password=****;”;

if (IsServerConnected(connectionString1))
{

string appPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
string configFile = System.IO.Path.Combine(appPath, “App.config”);
ExeConfigurationFileMap configFileMap = new ExeConfigurationFileMap();
configFileMap.ExeConfigFilename = configFile;
System.Configuration.Configuration config = ConfigurationManager.OpenMappedExeConfiguration(configFileMap, ConfigurationUserLevel.None);

/***************Connection String Section***********************/
config.ConnectionStrings.ConnectionStrings[“MyConStr”].ConnectionString = connectionString1;
config.Save();
ConfigurationManager.RefreshSection(“connectionStrings”);

labelOutPut.Text = “Connection String 1 is available and updated”;
}
else if (IsServerConnected(connectionString2))
{

string appPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
string configFile = System.IO.Path.Combine(appPath, “App.config”);
ExeConfigurationFileMap configFileMap = new ExeConfigurationFileMap();
configFileMap.ExeConfigFilename = configFile;
System.Configuration.Configuration config = ConfigurationManager.OpenMappedExeConfiguration(configFileMap, ConfigurationUserLevel.None);

/***************Connection String Section***********************/
config.ConnectionStrings.ConnectionStrings[“MyConStr”].ConnectionString = connectionString2;
config.Save();
ConfigurationManager.RefreshSection(“connectionStrings”);

labelOutPut.Text = “Connection String 2 is available and updated”;
}
else
labelOutPut.Text = “None of Connection Strings are available and updated”;

}

/***********************************************************************/

private void readConnStringSection()
{
/// <summary>
/// Read Connection Strings Section in App.Config
/// </summary>

string appPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
string configFile = System.IO.Path.Combine(appPath, “App.config”);
ExeConfigurationFileMap configFileMap = new ExeConfigurationFileMap();
configFileMap.ExeConfigFilename = configFile;
System.Configuration.Configuration config = ConfigurationManager.OpenMappedExeConfiguration(configFileMap, ConfigurationUserLevel.None);

string conStrValue=config.ConnectionStrings.ConnectionStrings[“conStrFirst”].ConnectionString;

}

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: