简体   繁体   中英

app.config file C# winforms preserve the connection string

I have an app.config file in Winforms application that holds a connection string. This is to go out to multiple tenant (clients) as a separate file. These clients have different database sources. This config file also holds other version information such as EF, Telerik reporting etc...

<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.1" />

and

<section name="Telerik.Reporting"  
         type="Telerik.Reporting.Configuration.ReportingConfigurationSection, Telerik.Reporting, Version=8.1.14.804, Culture=neutral, PublicKeyToken=a9d7983dfcc261be" 
         allowLocation="true" allowDefinition="Everywhere" />

The problem I have is when we have an updated version of EF or Telerik reporting with our application and we deploy (auto-deploy) this we need to overwrite the app.config file in the client directory to update the versions in the client config file. They then lose their connection setting and I do not want the client to have to go and re-enter it.

My question:

Is there a best practice to overcome this issue? Should I hold the connection string somewhere else?

Yep, the best thing to do is to move your connection strings section to an another config file and reference that file within your app.config.

For example create a new file called connectionStrings.config:

<connectionStrings>
  <add name="Default" connectionString="[client_connection_string] "/>  
</connectionStrings>

And in your app.config file:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <connectionStrings configSource="connectionStrings.config" />  
</configuration>

A full example can be found here .

Use an external configuration file that is referenced from the application config file. Eg include this section in your config file.

<configuration>
    <connectionStrings configSource="connections.config"/>
</configuration>

The external config file is described http://msdn.microsoft.com/en-us/library/ms254494(v=vs.110).aspx

Note that storing connection settings in plaintext on a workstation is still a bad idea.

Using Windows registry for stuff like this is a definite no-no these days.

您可以尝试将所需的所有连接数据保存在单独的xml文件中,以便在执行更新版本的部署时不会被覆盖。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM