views:

271

answers:

3

I have a console application which gets the connectionstring as a parameter. I would have to set a ConnectionString in app.config with name 'ConnectionString' and the given parameter as the sql connectionstring.

Thx to answers. With help of the links I got to this:

var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); var connectionStringSettings = new ConnectionStringSettings("ConnectionString", _arguments["connectionString"], "System.Data.SqlClient"); config.ConnectionStrings.ConnectionStrings.Add(connectionStringSettings); config.Save(ConfigurationSaveMode.Modified); ConfigurationManager.RefreshSection("connectionStrings");

A: 

Check out this article How to add a connection string to the web.config...

Vedran
The concept is a bit different for app.config files.
JonH
In what way? (why does a comment require at least 15 characters, "In what way" isn't enough?)
Vedran
A: 

Absolutely

Check this thread:

http://www.eggheadcafe.com/community/aspnet/14/10038785/modify-connectionstring-i.aspx

JonH
+4  A: 

Yes - have a look at this.

This method is probably what you are after

System.Configuration.ConfigurationManager.ConnectionStrings.Add(new ConnectionStringSettings("new name", "new string");
Matt Joslin