Windows Develop Bookmark and Share   
 index > Windows Forms General > app.config
 

app.config

i want send these connectionstring setting in app.config file at run time, how is this possible?


..............................................................................................
SqlConnectionStringBuilder

cb = new SqlConnectionStringBuilder();

true;

cb.ConnectTimeout = 15;


Please mark the replies as answers if they help and unmark if they don't.

cb.DataSource = textBox3.Text;

cb.InitialCatalog = textBox6.Text;

cb.IntegratedSecurity =

Nitin Chaudhary  Sunday, April 26, 2009 10:19 AM
//Add config like this:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>

</connectionStrings>
</configuration>


Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.ConnectionStrings.ConnectionStrings.Add(new ConnectionStringSettings(
"MyConnectionString",
String.Format("DataSource={0};InitialCatalog={1};IntegratedSecurity={2}",
"testing", "testing2", "Testing6")));
config.Save(ConfigurationSaveMode.Modified, true);
ConfigurationManager.RefreshSection("connectionStrings");
MessageBox.Show(ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString)
John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
JohnGrove  Sunday, April 26, 2009 3:58 PM
Thanks john,

If you want to modify an existing connection string.

You can programatically open the configuration with using the System.configuration namespace:

Configuration myConfig = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

Then you can access the connection strings collection at:

myConfig.ConnectionStrings.ConnectionStrings

You can modify the collection however you want, and when done call .Save() on the configuration object.


Thanks, A.m.a.L | [Remember to click "mark as answered" when you get a correct reply to your question]
A.m.a.L - aditi.com - Think Product  Sunday, April 26, 2009 4:16 PM
//Add config like this:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>

</connectionStrings>
</configuration>


Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.ConnectionStrings.ConnectionStrings.Add(new ConnectionStringSettings(
"MyConnectionString",
String.Format("DataSource={0};InitialCatalog={1};IntegratedSecurity={2}",
"testing", "testing2", "Testing6")));
config.Save(ConfigurationSaveMode.Modified, true);
ConfigurationManager.RefreshSection("connectionStrings");
MessageBox.Show(ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString)
John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
JohnGrove  Sunday, April 26, 2009 3:58 PM
Also,

1.) Add a manual reference to System.Configuration
2.) Then add the using statement

using System.Configuration;

Note:
In debug mode, you shouldn't notice the actual changing of the file. Running the exe directly, you will see the physical change occur.

John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
JohnGrove  Sunday, April 26, 2009 4:07 PM
Thanks john,

If you want to modify an existing connection string.

You can programatically open the configuration with using the System.configuration namespace:

Configuration myConfig = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

Then you can access the connection strings collection at:

myConfig.ConnectionStrings.ConnectionStrings

You can modify the collection however you want, and when done call .Save() on the configuration object.


Thanks, A.m.a.L | [Remember to click "mark as answered" when you get a correct reply to your question]
A.m.a.L - aditi.com - Think Product  Sunday, April 26, 2009 4:16 PM
Notice that A.m.a.L said "You can programatically open the configuration with using the System.configuration namespace"

Good subtle point and that wasn't a typo, when you manually add a reference to System.C onfiguration, you will see in your references System.c onfiguration [Note the lowercase].

But make sure at this point you add the proper using statement to the uppercase:

using System.Configuration;
John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
  • Edited byJohnGrove Sunday, April 26, 2009 4:26 PM
  •  
JohnGrove  Sunday, April 26, 2009 4:23 PM
Thanks John for validating.

Thanks, A.m.a.L | [Remember to click "mark as answered" when you get a correct reply to your question]
A.m.a.L - aditi.com - Think Product  Sunday, April 26, 2009 4:33 PM

You can use google to search for other answers

Custom Search

More Threads

• Progress Bar Redraw With New Value
• wanna change the name of columns to arabic but still have english name on Column query names ???
• Add exe to System tray
• Hide DataGridView Header Column Divider Bar
• WebBrowser needed, must be at least IE 5.5 SP1
• Expand/Collapse a category in property grid
• WebBrowser Control Displays HTML Tags Instead of Formatted HTML
• Throttling a Windows Form Application
• RTF Text box revisited
• Radiobutton Focus Problem