Introduction
We always use connection string in forms that we design but when you design a big software you should set connection string for once and use it in many forms in this method you use name of connection string in stead of connection string text.
Every time you want to change the connection string just you change the main connection string in App.Config file.
By this method you don't need to change all of forms in your project and just change the connection string in App.Config.
Using the Code
First of all you should set the connection string in App.Config File.
For example I set the connection string for my database as you see down:
<?xml version="1.0"?>
<configuration>
<connectionStrings>
<add name="CharityManagement"
connectionString="Data Source=.;Initial Catalog=CharityManagement;Integrated Security=True"/>
</connectionStrings>
</configuration>
After that you use connection string in your forms of project by this code:
In your forms you set references that you want to use.
using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
Then you can get Connection String from App.Config by use ConnectionStrings properties.
var connectionString=ConfigurationManager.ConnectionStrings["CharityManagement"].ConnectionString;
You can use this way in both of C# and ASP.NET Projects.