First you might want to add
using System.Configuration;
To your .cs file. If it not available add it through the Project References as it is not included by default in a new project.
This is my solution to this problem.
First I made the ConnectionProperties Class that saves the items I need to change in the original connection string.
The _name variable in the ConnectionProperties class is important to be the name of the connectionString
The first method takes a connection string and changes the option you want with the new value.
private String changeConnStringItem(string connString,string option, string value)
{
String[] conItems = connString.Split(';');
String result = "";
foreach (String item in conItems)
{
if (item.StartsWith(option))
{
result += option + "=" + value + ";";
}
else
{
result += item + ";";
}
}
return result;
}
You can change this method to accomodate your own needs. I have both mysql and mssql connections so I needed both of them. Of course you can refine this draft code for yourself.
private void changeConnectionSettings(ConnectionProperties cp)
{
var cnSection = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
String connString = cnSection.ConnectionStrings.ConnectionStrings[cp.Name].ConnectionString;
connString = changeConnStringItem(connString, "provider connection string=\"data source", cp.DataSource);
connString = changeConnStringItem(connString, "provider connection string=\"server", cp.DataSource);
connString = changeConnStringItem(connString, "user id", cp.Username);
connString = changeConnStringItem(connString, "password", cp.Password);
connString = changeConnStringItem(connString, "initial catalog", cp.InitCatalogue);
connString = changeConnStringItem(connString, "database", cp.InitCatalogue);
cnSection.ConnectionStrings.ConnectionStrings[cp.Name].ConnectionString = connString;
cnSection.Save();
ConfigurationManager.RefreshSection("connectionStrings");
}
As I didn't want to add trivial information I ommited the Properties region of my code. Please add it if you want this to work.
class ConnectionProperties
{
private String _name;
private String _dataSource;
private String _username;
private String _password;
private String _initCatalogue;
/// <summary>
/// Basic Connection Properties constructor
/// </summary>
public ConnectionProperties()
{
}
/// <summary>
/// Constructor with the needed settings
/// </summary>
/// <param name="name">The name identifier of the connection</param>
/// <param name="dataSource">The url where we connect</param>
/// <param name="username">Username for connection</param>
/// <param name="password">Password for connection</param>
/// <param name="initCat">Initial catalogue</param>
public ConnectionProperties(String name,String dataSource, String username, String password, String initCat)
{
_name = name;
_dataSource = dataSource;
_username = username;
_password = password;
_initCatalogue = initCat;
}
// Enter corresponding Properties here for access to private variables
}