Results 1 to 5 of 5

Thread: Updating conn string at run time

  1. #1

    Thread Starter
    Fanatic Member venerable bede's Avatar
    Join Date
    Sep 2002
    Location
    The mystic land of Geordies
    Posts
    1,018

    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.

    Parksie

  2. #2
    Frenzied Member
    Join Date
    Feb 2008
    Location
    Texas
    Posts
    1,288

    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
    You down with OOP? Yeah you know me!
    MCAD and MCMICKEYMOUSE (vb.net)

    ----

    If it even kinda helps... rate it : )

    Edit a Multi-page .tif file and save.

  3. #3

    Thread Starter
    Fanatic Member venerable bede's Avatar
    Join Date
    Sep 2002
    Location
    The mystic land of Geordies
    Posts
    1,018

    Re: Updating conn string at run time

    That wont update the config file though. Or at least I dont think it will.

    Parksie

  4. #4
    Frenzied Member
    Join Date
    Feb 2008
    Location
    Texas
    Posts
    1,288

    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
    You down with OOP? Yeah you know me!
    MCAD and MCMICKEYMOUSE (vb.net)

    ----

    If it even kinda helps... rate it : )

    Edit a Multi-page .tif file and save.

  5. #5
    Smitten by reality Harsh Gupta's Avatar
    Join Date
    Feb 2005
    Posts
    2,938

    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");
    Show Appreciation. Rate Posts.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width