Search code examples
c#app-config

How do I overwrite the default settings value in the settings designer for my Connection String


I need to use a different connection string than the one in the default settings value in the settings designer in order to run on my staging PC.

I have in settings.designer.cs:

  [global::System.Configuration.DefaultSettingValueAttribute("Data Source=MyServer\\MyDB;Initial Catalog=MyDB;Integrated Security=True;Pers" +
    "ist Security Info=True")]

public string MyConnectionString {
    get {
        return ((string)(this["MyConnectionString"]));
    }
}

My app.config is this:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
    <clear />
    <add name="MyConnectionString " connectionString=Data Source=MyServer2\\MyDB;Initial Catalog=MyDB;;Trusted_Connection=True"
      providerName="System.Data.SqlClient" />
</connectionStrings>
  <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
    </startup>
</configuration>

I want the application to use the settings in app.config but it persists in using the settings in the settings designer file.

I have tried various permutations of the following as well:

 <add name="AppName.Properties.Settings.Default.MyConnectionString" connectionString=Data Source=MyServer2\\MyDB;Initial Catalog=MyDB;;Trusted_Connection=True"
      providerName="System.Data.SqlClient" />

such as:

AppName.Properties.Settings.GlobalReference.Default.MyConnectionString

and:

ApplicationSettings.AppName.Properties.Settings.GlobalReference.Default.MyConnectionString

Any thoughts or ideas would be greatly appreciated.


Solution

  • In order for this to work, you have to have System.Configuration in your references.