.net 3.5: To read connectionstring from app.config?

Please see Reading Connection Strings in Web.Config and App.Config and Enterprise Library DAAB Settings (on the Wayback Machine as the original got deleted)

ConnectionStringSettings connection = ConfigurationManager.ConnectionStrings["MyConnectionString"]
string connectionString = connection.ConnectionString

You may need to add an assembly reference to System.Configuration


In the config:

<add name="ConnectionName" connectionString="Data Source=xxxx\yyyy;Initial Catalog=MyDB;User ID=userName;Password=pwd" />

In C# code:

    using System.Configuration;

...

    string connectionString = ConfigurationManager.ConnectionStrings["ConnectionName"].ToString();

Better still would be to define a function and use it in the code everywhere:

public string getConnectionStringMyDB()
        {
            return ConfigurationManager.ConnectionStrings["ConnectionName"].ToString();
        }

If name is a string value that represents the name of the connection string:

var connectionString = 
    ConfigurationManager.ConnectionStrings[name].ConnectionString;

In your example you didn't provide a value for name, so you'll have to do that before it'll work.