0

I always forget to change connection string when publishing Winforms application.

I'm using EF6 and .net framework 4.5.

In App.config

<connectionStrings>
<add name="NarudzbeEntities" connectionString="metadata=res://*/NarudzbeModel.csdl|res://*/NarudzbeModel.ssdl|res://*/NarudzbeModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=192.168.1.XX;initial catalog=Narudzbe;persist security info=True;user id=USER;password=PASSWORD;MultipleActiveResultSets=True;App=EntityFramework&quot;"
  providerName="System.Data.EntityClient" />
<add name="Narudzbe.Properties.Settings.NarudzbeConnectionString"
  connectionString="Data Source=192.168.1.XX;Initial Catalog=Narudzbe;Persist Security Info=True;User ID=USER;Password=PASSWORD"
  providerName="System.Data.SqlClient" />
</connectionStrings>

How can I have two connection strings in app.config or what would be correct way of doing that.

There are many articles for ASP.NET web.config but I need solution for winforms.

Carlo
  • 13
  • 5

3 Answers3

1

What I currently use is an App.config per configuration. So in your use case that would be an App.Debug.config and an App.Release.config.

For this to work you have to edit the project file. In dotnet framework using visual studio you have to unload you project.

In dotnet core or framework sdk style project (the dotnet core style project) you don't have to.

Add the following snippet:

<PropertyGroup>
  <AppConfig>App.$(Configuration).config</AppConfig>
</PropertyGroup>

Now what ever configuration you run your code in, that app.config will be used.

I found this in the following answer on this thread. For me this works using winforms running on dotnet framework 4.8 while using ef core 3.1.

Chevalric
  • 79
  • 7
  • This is awesome! I was looking for a way to make VS make my "App.config" copy to the output and your solution made me do `App.config`. With the `` XML node, this led me to the right direction. Thanks a lot. – Uwe Keim Jun 28 '23 at 14:35
0

You can do something like this in app config.Using the ConnectionStrings section of the configuration file link has nice information to follow.

<configuration>
<connectionStrings>
    <clear/>
    <add name="Dev"
         providerName="System.Data.SqlClient"
         connectionString="Data Source=machine\sq; Initial Catalog=DevDB; Integrated Security=SSPI"
/>
    <add name="QA"
         providerName="System.Data.SqlClient"
         connectionString="Data Source=machine\sql; Initial Catalog=QADB; Integrated Security=SSPI"
/>
    <add name="Prod"
         providerName="System.Data.SqlClient"
         connectionString="Data Source=machine\sql; Initial Catalog=ProdDB; Integrated Security=SSPI"
/>
</connectionStrings>

0

If you are using EF, You must use connection string in app config with the same name as your Context class. I had problems when I tried to pass connection string name via base constructor of Context class. So just remark connection strings in app config when you are publishing your app.

Elishal
  • 1
  • 2