1

I am working with the existing structures used by the dev team at my current company. In order to change connection strings within the application, the team creates an XML node called "defaultDatabase" (see below):

  </configSections>
<dataConfiguration defaultDatabase="Development" />
  <connectionStrings>
<add name="Development" connectionString="Data source=DVHQSQL01; Initial Catalog=db; User ID=id; Password=password"/>
  </connectionStrings>

...there will be more connection strings in the XML for different server environments (not shown here, but "Testing", "Staging", "Production", etc)

I am creating a class with different functions that use the default connection. I know one route is through ConfigurationManager as shown here:

https://social.msdn.microsoft.com/Forums/en-US/9a8c9f5a-092e-4c4a-87bb-9f35d8f55da1/get-connection-string-from-appconfig-file?forum=adodotnetdataproviders

This is great if you can change the connection string name in code, but we use the defaultDatabase node to be able to change from different environments without rebuilding.

Does anyone have experience with this methodology? How do I get the correct connection string using the defaultDatabase node?

5
  • What about <connectionStrings> <add name="default" .... We use this since years in a multi Connection String environemnt. Commented Apr 11, 2016 at 15:25
  • You really should edit your question and remove your password and user id from the connection string. Just replace it with something like <mypassword> because anyone who comes to this page can see that. Commented Apr 11, 2016 at 15:27
  • @Icemanind edited. Commented Apr 11, 2016 at 15:29
  • @AlexB. I love this idea and will explore it further, thank you! Commented Apr 11, 2016 at 15:29
  • @AlexB. if you submit your idea as an answer, I can mark it as correct. You solved my issue in under a minute. Commented Apr 11, 2016 at 15:33

2 Answers 2

3

Just set the default connection in the connection strings area:

<connectionStrings>
   <add name="default" connectionString="..."/>
</connectionStrings>

If you have further connections you can add them in the same way:

<connectionStrings>
   <add name="default" connectionString="..."/>
   <add name="special1" connectionString="..."/>
   <add name="special2" connectionString="..."/>
</connectionStrings>
Sign up to request clarification or add additional context in comments.

Comments

0

You can have as many connectionStrings as you want in your config, all you have to do is give them different names.

<connectionStrings>
    <add name="default" connectionString=""/>
    <add name="anotherOne" connectionString=""/>
    ...
      </connectionStrings>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.