First of all you should set the connection string in the App.Config file. For example I set the connection string for my database as you see here
<configuration>
<connectionstrings>
<add name="TestConnectionstring"
connectionString="Data Source=.;Initial Catalog=CharityManagement;Integrated Security=True"/>
</add></connectionstrings>
</configuration>
After that you use the connection string in your forms using 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 the Connection String from the App.Config by using the ConnectionStrings property.
var connectionString=ConfigurationManager.ConnectionStrings["TestConnectionstring"].ConnectionString;
You can use this method in both Windows Forms and ASP.NET projects.