12

I have a c# assembly that uses the app.config to store its database connection string. When debugging the application I noticed that the connection to the database kept failing because the ConfigurationManager kept returning the machine.config connection string:

data source=.\SQLEXPRESS; Integrated Security;....

I added <clear/> before my connection string in the app.config and it fixed the issue on my dev machine. The problem returned when I deployed it to production. Can someone tell me how I can stop the machine.config connection string from being used?

SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings[0].ConnectionString);

<connectionStrings>
    <clear/>
    <add name="VersionConnectionString"
     connectionString=" Data Source=localhost;Initial Catalog=VersionInfo;User ID=user;Password=password"         
     providerName="System.Data.SqlClient" />
  </connectionStrings>

UPDATE

The following still gives me the machine.config connection string?!

 Configuration appConfig = ConfigurationManager.OpenExeConfiguration(Assembly.GetExecutingAssembly().Location);
                string dllConfigData =
                    appConfig.ConnectionStrings.ConnectionStrings[0].ConnectionString;
3
  • I know it is an old post, but still I am curious, is this a desktop or console application? Do see the connection string section with the clear element in the output (bin) directory? Commented Oct 22, 2015 at 8:33
  • I still have the problem in my application. I added clear, or used below codes and copy config file to exe folder but still it is fetching default data. I am using VS2015. Commented Apr 25, 2016 at 10:05
  • I had the same problem. I had the right connection string in the 'class library' project I used and left the 'web.config' empty in the 'Web' project. As soon as I added the same in the web project as well, it worked fine for me. Just in case if you have multiple projects in a solution Commented Apr 16, 2017 at 21:38

5 Answers 5

6

When using connection strings in a DLL, you need to add them to your exe's app.config as well, using the exact same name for the setting. Then, you can change the connection string in the exe's .config file and the DLL will pick it up automatically when loaded.

This is probably the only way you can have working custom connection strings in the app.config file when your DB persistence layer is implemented in a separate DLL. Don't even ask me how much time it took me to research and debug this.

Sign up to request clarification or add additional context in comments.

Comments

3

I know this is an older question, but I was having the same problem today. The problem was that the app.config that I added to my project wasn't being copied to the output directory. To fix this, right click on the app.config and select Properties. Then change Build Action to Content. Hope this helps!

Comments

1

Try getting an instance of your app.config file as a Configuration object:

var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

var myConnString = config.ConnectionStrings["VersionConnectionString"].ConnectionString;

This bypasses the machine config file completely.

3 Comments

This is a class library (.dll) OpenExeConfig throws and exception. Good idea though.
That doesn't seem to actually bypass the machine config
How get ONLY connecttionstrings from app.config/web.config, NOT machine.config? In the machine.config is a connection string that is called "LocalSqlServer"; the default provider (as expected) is "System.Data.SqlClient", and the connection string is data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true",
0

You should be getting your connection string by NAME instead of INDEX - that will ensure you're getting what you're asking for.

Try

SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["VersionConnectionString"].ConnectionString);

1 Comment

you get your machine.config connection string when you get it by name?
0

The way our team overrides the values from the .NET machine configuration file (machine.config) is by using the 'remove' tag in the application configuration file.

<connectionStrings>
    <remove name="VersionConnectionString"/>
    <add name="VersionConnectionString"
     connectionString=" Data Source=localhost;Initial Catalog=VersionInfo;User ID=user;Password=password"         
     providerName="System.Data.SqlClient" />
</connectionStrings>

The application reads the values in the machine configuration file first and then it reads the values from the application configuration file. The .Net Framework doesn't like to have the same name twice, and it will complain about it. Using "remove" prevents duplicate names being loaded.

If the 'name' is not present in the configuration file at all, nothing happens. The application doesn't complain about it.

Make 'remove' your friend, but use it appropriately.

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.