|
-
Mar 17th, 2008, 09:53 AM
#1
Thread Starter
Fanatic Member
Updating conn string at run time
I dont get this.
I have figured out how to read then update settings in my app.config at run time using the following exert:
Code:
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
ConfigurationSectionGroup grpApp = config.GetSectionGroup("applicationSettings");
ClientSettingsSection secPropSet = (ClientSettingsSection)grpApp.Sections[sSettingsSection];
secPropSet.SectionInformation.ForceSave = true;
SettingElement elm = secPropSet.Settings.Get(SettingName);
elm.Value.ValueXml.InnerText = NewValue.Trim();
config.Save()
This works fine with custom sections but not with <connectionStrings>.
Reading the connection strings can be done using the following exert :
Code:
//conn strings
ConnectionStringsSection connStringSection = config.ConnectionStrings;
ConnectionStringSettingsCollection connectionStringsCollection = connStringSection.ConnectionStrings;
System.Collections.IEnumerator connStringsEnum = connectionStringsCollection.GetEnumerator();
int i = 0;
while (connStringsEnum.MoveNext())
{ ///blah blah
}
For the life of me I cannot figure out how to write an update made against a connection string.
Last edited by venerable bede; Mar 17th, 2008 at 09:56 AM.
-
Mar 17th, 2008, 10:39 AM
#2
Frenzied Member
Re: Updating conn string at run time
I don't understand,
if you have a connection object, and it's connectionString is set, cant you just modify it later at runtime?
using its "myConnection.ConnectionString()" property?
Justin Fox
-
Mar 18th, 2008, 08:22 AM
#3
Thread Starter
Fanatic Member
Re: Updating conn string at run time
That wont update the config file though. Or at least I dont think it will.
-
Mar 18th, 2008, 03:19 PM
#4
Frenzied Member
Re: Updating conn string at run time
Oh, why do you need it to update the config file? I thought you just needed to connect to a different database with the same connection for some reason.
Justin Fox
-
Mar 18th, 2008, 04:07 PM
#5
Re: Updating conn string at run time
Assuming that you are using 2005 (I am not sure if it works in earlier version(s)).
Code:
Configuration config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
ConnectionStringSettings connStringSettings = new ConnectionStringSettings("testName", "server=testServer;Initial Catalog=testDB;blah blah");
config.ConnectionStrings.ConnectionStrings.Add(connStringSettings);
config.Save(ConfigurationSaveMode.Minimal, false);
ConfigurationManager.RefreshSection("connectionStrings");
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|