tags:

views:

88

answers:

2

In my C#-WinForms-application (.Net 3.5, VS2008) I read the App.Config using:

Configuration myConfig = 
  ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

App.config contains two connection strings in the connectionStrings-Section:

<connectionStrings>
    <add name="db1" connectionString="connection1"
     providerName="System.Data.SqlClient"/>
    <add name="db2" connectionString="connection2"
      providerName="System.Data.SqlClient" />
</connectionStrings>

(connection strings abreviated for readability)

But when I loop through the ConnectionStringsCollection I find that it has three entries instead of the expected two. The additional entry points to a database called "aspnetdb.mdf" on a local SqlExpress-Server. I can't remember adding this anywhere, I can't find it as a literal value in my project files. Watching the collection with the debugger I find that my connection strings have the app.config as source. The additional string has null as source, so I think .net conjurs it up from somewhere.

How can I get rid of this entry, or failing that how can I tell the configuration manager to only use the connection strings in the app.config?

+2  A: 

The aspnetdb.mdf comes from the default connection strings collection, remove it by adding

<clear />

to the top of your element.

Stephen Newman
+6  A: 

Did you check machine.config?

(Found in %SystemRoot%\Microsoft.NET\Framework\v2.0.50727\CONFIG for 64bit use Framework64.)

Binoj Antony
Probably correct. Had the same issue when taking the first connection, but one of our team failed to run the code. He had an obscure oracle tool installed, that's connection string survived the deinstallation inside the machine config (what a surprise, since the Oracle installers are so well-crafted ;-)
Marc Wittke
This was correct, found the entry there. Thanks a lot. However, I went with Stephen Newmans answer: by adding the "clear" on top of my section I can make sure that the end user of my application only hats the connections from the app.config.
Stephan Keller