Is it possible to change ConnectionStrings configuration at runtime?

Is it possible to change the connection strings defined in app.config / web.config at run time? I want to use different configuration files depending on which computer the application / site is running on (of course, for debugging purposes. We will use regular configuration files for deployment).

I can write AppSettings, but not ConnectionStrings (AFAIK). Or can I?

+1
source share
5 answers

Yes, it is possible, but AFAIK only through Reflection. The following code should do what you need (read below for use):

public static string SetConnectionString(Type assemblyMember,
                                         Type settingsClass,
                                         string newConnectionString,
                                         string connectionStringKey)
{
  Type typSettings = Type.GetType(Assembly.CreateQualifiedName(assemblyMember.Assembly.FullName, settingsClass.FullName));

  if (typSettings == null)
  {
    return null;
  }

  PropertyInfo prpDefault = typSettings.GetProperty("Default", BindingFlags.Static | BindingFlags.Public);

  if (prpDefault == null)
  {
    return null;
  }

  object objSettings = prpDefault.GetValue(null, null);

  if (objSettings == null)
  {
    return null;
  }

  // the default property, this[], is actually named Item
  PropertyInfo prpItem = objSettings.GetType().GetProperty("Item", BindingFlags.Instance | BindingFlags.Public);

  if (prpItem == null)
  {
    return null;
  }

  object[] indexerName = { connectionStringKey };
  string oldConnectionString = (string)prpItem.GetValue(objSettings, indexerName);

  prpItem.SetValue(objSettings, newConnectionString, indexerName);

  return oldConnectionString;
}

assemblyMember -
settingsClass -
newConnectionString - connectionStringKey - , .

, Main().

+2

, , ( , , ) app.config , app.config .

, , app.config, , , .

, , app.config .

<connectionStrings>
    <add name="YourNameSpace.Properties.Settings.ConnectionString_1"
        connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data  
        Source=|DataDirectory|\file.mdb"
        providerName="System.Data.OleDb"/>

    <add name="YourNameSpace.Properties.Settings.ConnectionString_2"
        connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data  
        Source=|DataDirectory|\file.mdb"
        providerName="System.Data.OleDb"/>

</connectionStrings>

, ( :-)) :

YourNameSpace.Properties.Settings foo = new YourNameSapce.Properties.Settings();

foo.ConnectionString_1;

@nand

P.S: #.

+1

.

( ) machine.config master web.config( "" "location" ) - , .

- /, . , , "robocopy" .


" "; , - . , - ; "localserver" ( "localhost" ) DNS , . , IP- (, , DHCP), !

Ditto ; "."; , "devserver" , .

...

0

In this article, we will take a closer look at the options you have: http://aspalliance.com/820

0
source

You can run xmlpoke in an NAnt script when installing the website.

eg.

  • Deploy NAnt with a website.
  • Create go.bat, which calls NAnt, which installs the site and runs xmlpoke, to modify the web.config file with the settings based on the server name or other parameter.
0
source

Source: https://habr.com/ru/post/1723622/


All Articles