How to change application connection string?

I've learned how to build connectionstrings and read them from the configuration classes, but I'm still confused.  Every dataset I create using the visual database tools uses the connectionstring stored in the application settings.  This is great until I want to change it.  Since it's a read only property, I can't directly change it to point from my development machine to a production machine in the code.  How do I do this

 

 



Answer this question

How to change application connection string?

  • Andrew Williams

    I have the same question.  Did you learn how to change the connection string   I have a Data Access DLL with a DataSet.  The DataSet has 5 Tables and TableAdapters.  The connection string that VS2005 used to create these objects in in the app.config for the DLL.  I understand that the designer needs to save the string it uses during design, but how do I change all the TableAdapter connection string at runtime

    My client Windows Application has a config file with  the connection string I want to use.  I know how to read the connection string in that config file of the client from inside of the the DLL.

    What did you do


  • Mumtaz

    I so need to get on the ball with 2.0, still waiting on my MSDN subscription to get cleared up.

     

    Anyways, I'm sure that it's getting that setting from the appconfig right so just change the appconfig, it's just an XML, then everywhere that calls <project name>.Properties.Settings.Default.myConnectionString  will have the new conneciton string when ran at the client since that pulls from the app.config file.

    Hope this helps



  • SgtJake

    This probably isn't a great solution, but so far it's working.  You can change a tableadapter's connection string using the <adapter>.Connection.ConnectionString property.  In the constructor for each form, I set this property to a global connection string that I create at program startup.  Most of my forms only have a few tableadapters, so it's not a terrible burden, but it certainly isn't elegant.

  • billyzelsnack

    I found a link on the forums:
    https://blogs.msdn.com/smartclientdata/archive/2005/07/25/443034.aspx

    Basically I had a situation where I wanted to change the connectionstring that M$ store when using the funky project data sources way of coding.

    All I did is added this sub to the settings.vb file (instructions how to view the setting code in included in that link above)

    Private Sub MySettings_SettingsLoaded(ByVal sender As Object, ByVal e As System.Configuration.SettingsLoadedEventArgs) Handles Me.SettingsLoaded

    Me.Item("cnDatabase") = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Me.Item("UserSettingDatabaseLocation")

    End Sub

    Where cnDatabase is the name of the Connection you have used for your datasets, and UserSettingDatabaselocation was just a User setting that i have used, but you could set that to anything like your own XML based setting or anything.

    Good Luck!



  • Steve Swartz

    Well, in 2.0 I believe they changed how you work with appsettings, but I haven't played with that myself. In 1.1/1.0 (who knows which version your asking about based on your question), well in 1.0/1.1 you create an XMLDocument, you load the app.config file up doc.loadfrom(pathtofile). You then do a pathsearch, find your element and then change the value attibute.

    Hopfully this project can help

    http://www.planet-source-code.com/vb/scripts/ShowCode.asp txtCodeId=2424&lngWId=10

    happy holidays

    Joe



  • danycxxx

    I believie that Microsoft should provide an AppSettingsWriter class, which would implement methods for changing values in the appSettings section of the App.config file.
    Because Microsoft didn't think of variuos advantages coming from the ability of modyfing the App.config, I would like to present The AppSettingsWriter class.
    I haven't written it, but I found it some time ago, and tried to give you link, but couldn't google it :(
    Here's the code - it's trivial and works like a charm. If the specified element, of which we would like to change value isn't foune, it creates a new node.
    Brilliant:
    public class AppSettingsWriter
        {
            private string _configFileName;
            private XmlDocument _configDocument = new XmlDocument();

            public AppSettingsWriter()
            {
                Assembly asmy = Assembly.GetEntryAssembly();
                _configFileName = asmy.Location + ".config";
                _configDocument.Load(_configFileName);
            }

            public void SetValue(string key, string value)
            {
                string xpath =
                    string.Format("/configuration/appSettings/add[@key=\"{0}\"]", key);
                XmlNode node = _configDocument.SelectSingleNode(xpath);

                if(node == null)
                {
                    XmlElement element = _configDocument.CreateElement("add");
                    element.SetAttribute("key", key);
                    element.SetAttribute("value", value);

                    xpath = "/configuration/appSettings";
                    XmlNode root =
                        _configDocument.DocumentElement.SelectSingleNode(xpath);

                    root.AppendChild((XmlNode)element);
                }
                else
                {
                    node.Attributes.GetNamedItem("value").Value = value;
                }

                _configDocument.Save(_configFileName);
            }

            public string ReadNowValue(string key)
            {
                string xpath =
                    string.Format("/configuration/appSettings/add[@key=\"{0}\"]", key);
                XmlNode node = _configDocument.SelectSingleNode(xpath);

                if(node == null)
                {
                    throw new NullReferenceException();
                }
                else
                {
                    return node.Attributes.GetNamedItem("value").Value;
                }

                //_configDocument.Save(_configFileName);
            }
        }

  • madhu mudunuri

    Perhaps this will be more clear.  In VS2005, every time I create a new data source that I will later bind to a form, I am prompted to use the Data Source Configuration Wizard.  I choose database and the next screen configures the data connection.  If I create a new connection, it is stored in the Project's Properties.Settings as an application setting.  Every datasource I create subsequently uses this same connectionstring.  Here's a line from the <dataset>.Designer.cs file:

    this._connection.ConnectionString = global::<project name>.Properties.Settings.Default.myConnectionString;

    This is all great when I'm developing the application on my local machine.  Now, I want to deploy this to a customer's server.  I can create the string I want and store it in app.config.  I can read it out of app.config.  I can build it with SqlConnectionStringBuilder.  But, I can't assign it to <project name>.Properties.Settings.Default.myConnectionString because it is read only.

    So, my question is..how do I redefine <project name>.Properties.Settings.Default.myConnectionString at runtime so all my datasources will point to the correct server

     


  • How to change application connection string?