Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reference same Connection String in Multiple .NET Applications

I'm not sure if this is the correct medium for this question, so if it's not please inform me and I will correct.

As an example to illustrate my point, let's say I have 30 .NET applications that are published to 2-3 different web servers. Now, these applications are Intranet based, so I have a C# library that I reference in each of those applications. This library is comprised of methods that I use in each application to find out employee information if necessary. Now, the library has one connection string, but then I have to also reference that same connection string in all of the 30 applications... so if the database changes that the library is referencing, then I have to remember all 30 applications and change each of them individually.

Is there a way to put the connection string in some sort of file (text file, or something), and have each web.config in each of the 30 applications reference that specific file, so if the connection string needs to be changed, I can just change it in the file and then all 30 applications are still okay, or is there not something like that?

UPDATE

Okay, I now know how to reference a text file for connection string purposes.. but, it seems as though I only can do one of two options... either reference my primary connection string and library connection string individually within the web.config like so:

OPTION ONE

<connectionStrings>
  <add name="PrimaryCS" // more data />
  <add name="LibraryCS" // more data />
</connectionStrings>

This option would require me to change the LibraryCS connection string in each of the 30 applications if it were ever to be changed (NOT WHAT I WANT)

OPTION TWO

<connectionStrings configSource="MyConfig.config"></connectionStrings>

This option forces me to put both connection strings in the MyConfig.config file and not just the LibraryCS file.. so this would result in me having to change the LibraryCS connection string in every MyConfig.config file for each of the 30 applications (AGAIN, NOT WHAT I WANT).

WHAT I'M LOOKING FOR

I am looking for a mixture of the two options, but it seems this can't be done, so I'm hoping someone on this medium knows a work-around

<connectionStrings configSource="MyConfig.config">
  <add name="PrimaryCS" // more data />
</connectionStrings>

I want the MyConfig.config file to only hold the LibraryCS connection string and then have the main PrimaryCS connection string be separate.. so that if the LibraryCS connection string were ever needed to be changed, then I only have to do it in one place.

like image 441
Grizzly Avatar asked Nov 30 '18 20:11

Grizzly


People also ask

Where is .NET connection string stored?

Connection strings can be stored as key/value pairs in the connectionStrings section of the configuration element of an application configuration file.

How do I reference a connection string in web config?

config. To read the connection string into your code, use the ConfigurationManager class. string connStr = ConfigurationManager. ConnectionStrings["myConnectionString"].

What is difference between Appsettings and connection strings?

The main difference is in appsettings section we can store any data string values including database connection strings also but in connectionStrings section only database connection strings can store those are our application connection strings and new features (Membership, Personalization and Role Manager) connection ...


2 Answers

Microsoft has an amazing documentation about it. Hopefully that helps.

To store connection strings in an external configuration file, create a separate file that contains only the connectionStrings section. Do not include any additional elements, sections, or attributes. This example shows the syntax for an external configuration file.

<connectionStrings>  
  <add name="Name"   
   providerName="System.Data.ProviderName"   
   connectionString="Valid Connection String;" />  
</connectionStrings>

In the main application configuration file, you use the configSource attribute to specify the fully qualified name and location of the external file. This example refers to an external configuration file named connections.config.

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

Source : https://docs.microsoft.com/en-us/dotnet/framework/data/adonet/connection-strings-and-configuration-files

like image 53
Ayberk Avatar answered Nov 15 '22 15:11

Ayberk


One solution I came up with is to create a shared connection string file and reference it in the web.config of multiple web application projects.

So in my web configs I have this:

<connectionStrings configSource="configs\connectionStrings.local.config">

This file just contains the 'connectionStrings' portion of the config.

And I copy in the connectionStrings.local.config file into the configs folder on build of the project from a common solution folder (the config file is added to the solution as a solution item.

Other alternatives would be to use an environment variable on the servers to store the connection string or as people are mentioning a common file (just read the file as you would any text file).

like image 34
MikeS Avatar answered Nov 15 '22 15:11

MikeS