How to store and get database connection string from app.config file in WPF application in VS2012?


How to store and get database connection string from app.config file in WPF application in VS2012?

Storing and getting a database connection string from app.config is a very common practice used in every WPF application. I will make you understand this concept in following 3 steps:

Step 1: Store database connection string in your WPF app.config file like below:



 
                  connectionString="Data Source=myComputerName\sqlexpress;Initial  Catalog=MY_DB;User ID=user;Password=password/>
 



Step 2: Now include System.Configuration namespace in your application. But note that you also have to add reference of System.Configuration file in case if you are using Visual Studio 2012. Go to references and add System.Configuration namespace from there.

using System.Configuration;

Step 3: Add following line to get the database connection string in your class file. I have used ConfigurationManager to get the database connection string from app.config.

var connectionString=ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;

This concludes the article and hope you have understood the concept of how to store and get database connection string from app.config file in WPF application in VS2012?