Settings ConnectionString "ease of use"

is there any way change applicationsettings (Properties.Settings.Default.ConnectionString is readonly property) at runtime especially for the ConnectionString. It is common to write and test on one computer but deploy for other computers with other dbnames etc. ConnectionString as Application Setting (VS2005) is very practical but one has to be able to change the contents at run time. I have not discovered how. Of course one could just touch every object accessing a db and change its ConnectionString, but I was hoping there would be a much smarter approch than that.

Answer this question

Settings ConnectionString "ease of use"

  • Dietz

    CHange the scope of the setting to user vs application

    http://msdn2.microsoft.com/en-us/library/shytyc55.aspx

    • Application-scoped settings can be used for information such as a URL for a Web service or a database connection string. These values are associated with the application, so users cannot change them at run time.

    • User-scoped settings can be used for information such as remembering the last position of a form or a font preference. Users can change these values at run time.

    You can change the type of a setting using the Scope property.

    The project system stores application settings in two XML files: an app.config file, which is created at design time when you create the first application setting; and a user.config file, which is created at run time when the user running the application changes the value of any user setting. Notice that changes in user settings are not written to disk unless the application specifically calls a method to do so.



  • Michael_P2234

    Okay,

    - one can't change Application Settings in code which is obvious because all generated application settings properties property are "read only".

    - but one can change app.config: Application Settings Properties are stored there as well. Can this is done on startup before programm uses values contained thererin


  • vagrant

    Clarification

    The above solution is a way of doing what I had intended to do (set the ConnectionString property programmatically due to customer related constrictions (no .config)). It is NOT the way it shoudl be done (which would be to edit the config file!)


  • Jonathan MacCollum

    You can create two copy of app.config file, one for debugging and another for deployment. Before you deploy your app, you just copy over the deployment app.config File.



  • mike_morrison

    the correct answer follows (sorry I got slightly lost on the vb thread, I do c#) but annswer doesn't change:

    one soultion: add partial class to Properties and add e.g. DBString property that writes to internal ConnectionString. At program startup just "fix" Properties.Settings.Default.ConnectionString by setting Properties.Settings.Default.DBString that writes "through" to this["ConnectionString"]

    e.g.

    using System;
    using System.Collections.Generic;
    using System.Text;

    namespace XYZ.Properties {
    internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase {
    internal string DBString {
    get { return ConnectionString; }
    set { this["ConnectionString"] = value; }
    }
    }
    }

    uhrg. but this is the way it will work!


  • Settings ConnectionString "ease of use"