16

Please see the code below:

Public Shared Sub SyncConnectionStrings()
    Dim strCon As String
    Dim tyDatabase As typeDatabase
    Dim boolConfigChanged As Boolean
    'Dim config As System.Configuration.Configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
    Dim config As Configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
    Dim strConnectionString As ConnectionStringSettings            
    For Each strConnectionString In ConfigurationManager.ConnectionStrings
        'More code here, but irrelevant for this question.
    Next
End Sub

The code loops through all the connection strings in the app.config file. There are three connection strings in the app.config, but four connection strings are found. Where is: LocalSqlServer (this is the ConfigurationManager.ConnectionStrings.Name) defined?

2 Answers 2

18

It is defined in the Machine.config global file located at

"C:\Windows\Microsoft.NET\Framework\v4.0.30319\CONFIG".

It is generated when you install the NET.Framework and is needed by tools that work on the ASPNETDB

If you don't need it, you could try to add this to your app.config

<connectionStrings>
<clear/>
.....

or also

<connectionStrings>
<remove name="LocalSqlServer" />

I really suggest to not change anything in Machine.Config unless you know the side effects.

2
  • 2
    Thanks +1. Do you know if there is a way of looping around the app.config connection strings only?
    – w0051977
    May 7, 2013 at 10:39
  • Answer updated, not really sure of the side effects, but you could try and if anything goes wrong restore the intial status
    – Steve
    May 7, 2013 at 10:46
8

It is possible to have more than just the LocalSqlServer in your Machine.config. e.g. I just ran my app on a computer with a MySql connection in Machine.config

You can use the ElementInformation property of the ConnectionStringSetting to determine where it came from:

foreach (ConnectionStringSettings c in ConfigurationManager.ConnectionStrings)
{
    ElementInformation elementInfo = c.ElementInformation;
    String source = elementInfo.Source;

    if (source == null)
    {
        //Machine.config
    } 
    else
    {
        Console.WriteLine("{0} {1} {2}", c.Name, elementInfo.Source, c.ConnectionString);
    }
}

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.