Monday, December 7, 2009

Define connection strings in app.config

If you only need one connection string, you can define this in App.config:
<appSettings>
<add key="ConnectionString" value="Server=mysql01;Database=...."/>
</appSettings>
And use ConfigurationSettings.AppSettings["ConnectionString"] to retrieve the value.

For multiple connection strings, you can define multiple keys in AppSettings.
Alternatively, you can add a ConnectionStrings group in the App.config file.
<connectionStrings>
<add name="MyConnectionString1" connectionString="Server=mysql01;Database=...."/>
<add name="MyConnectionString2" connectionString="Server=mysql01;Database=...."/>
</connectionStrings>
Then use ConfigurationManager.ConnectionStrings["MyConnectionString1"].ConnectionString to retrieve the value.
*Note that you need to explicitly add a reference to System.Configuration (v2.0) in order for VS to detect that ConfigurationManager is a valid class.


No comments:

Post a Comment