Here's another area where the documentation that comes with Visual Studio 2005 and the suff online as MSDN make a simple topic complicated.
To add a connection string to the App.Config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="aConnectionString"
connectionString="Data Source=localhost;Initial Catalog=Northwind;Integrated Security=True"
providerName="System.Data.SqlClient"/>
</connectionStrings>
</configuration>
To retrieve it in your program:
1. Add a reference to System.Configuration (no this is not automagically included).
2. Add a using statement:
using System.Configuration;
Optionally add another using statement for the sqlclient:
using System.Data.SqlClient;
3. Use this code to grab the connection string:
string cnString = ConfigurationManager.ConnectionStrings["aConnectionString"].ConnectionString;
SqlConnection cn = new SqlConnection(cnString);
Note: You can also add the connection string information to the connectionStrings section of the machine.config. This is handy if you have a machine that runs many automated script type programs.