Read Database Connection Strings from App.Config

Read Database Connection Strings from App.Config

Storing database connection strings in source code can lead you to some security issues and can also cause you maintenance problems. It is always recommended that you keep your connection strings in a separate configuration file so that you can change database connection related information such as password or database name without even modifying or recompiling your source code.

.NET Framework 2.0 added a separate configuration section in both Windows Application configuration (App.config) and Web Application configuration (Web.config) file. Developers can use these section to store connection string information such as a connection string name or provider type etc. In the following tutorial I will show you how you can store and retrieve connection string information in .NET Windows Application using C#.

The following code shows how you can store connection strings in App.config file.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
   <connectionStrings>
      <add name="MyDBConnectionString" providerName="System.Data.SqlClient"
            connectionString="Data Source=localhost;Initial Catalog=MySQLServerDB; Integrated Security=true" />
   </connectionStrings>
</configuration>

Once you have saved your connection string in App.config file you can use System.Configuration.ConfigurationManager class to read this connection string in code.

ConnectionStringSettings  conSettings = ConfigurationManager.ConnectionStrings["MyDBConnectionString"];

ConnectionStringsSettings class provides properties to read connection string settings in your program as following code shows.

string name = conSettings.Name; 
string providerName = conSettings.ProviderName;
string connectionString = conSettings.ConnectionString;
READ ALSO:  Introducing Windows Communication Foundation (WCF)

This Post Has 17 Comments

  1. Tabassum javaid

    Hi
    How I modify my app.config using vb .net

  2. Arun

    In the above post you have explicitly explained reading connection string from the app file. Could you please help me how to, programmatically, write a connection string into app.config file. Please provide the code in visual basic.net.

  3. raghav

    Hello

  4. Umar

    Very nice tutorial.. Thanks! nicely explained

  5. appsoft

    gud one thanks alot:)
    It works

  6. Jagatpal Singh

    How to configure database path setting in .inf file
    for client users to access them…………

  7. Mo

    Thank you!

  8. Gentleman

    please more elaborate it , specially.
    ConnectionStringSettings conSettings = ConfigurationManager.ConnectionStrings[“MyDBConnectionString”];
    why did you use ” [“MyDBConnectionString”] ” .
    thanks.

    ssssvvvvvs

    1. Waqas Anwar

      Hi Gentleman

      The name of the connection string I have defined in configuration file is MyDBConnectionString that’s why I am using MyDBConnectionString to read connection string from configuration file.

  9. Gentleman

    please more elaborate it , specially.
    ConnectionStringSettings conSettings = ConfigurationManager.ConnectionStrings[“MyDBConnectionString”];
    why did you use ” [“MyDBConnectionString”] ” .
    thanks.

  10. Adnan Ghaffar

    nice

  11. mukharanja

    I worked on your code, but class ConfigurationManager is not recoginzing .
    throwing error ConfigurationManager not exists.
    Is there any dll or assembly has to download and install to solve this error?
    thanks

  12. John

    What about for MS Access database can we use this process.

  13. Sankar

    Nice but need more clarification

  14. satya06.ag

    The code for storing connection string in app.config file is very helpful for me. And also it is very simpl to understand.

    Thank you for this solution.

    I have one more request that please send me the settings for connection string in web.config file for asp.net and also the settings for login as i wanted to display my login page for all the request which directly coming from browser.

    Waiting for reply.

    Thank you

  15. Vuyiswa

    Am Using VB 2003, and the ConfigurationManager is not Recognised. and am not doing a Web Application , its a Windows Application. How can i retrieve the Connection string in my Win App

    Thanks

  16. luckykabutar

    thank you, lovely article

Leave a Reply