Thursday, April 12, 2007

Getting connection string from app.config in .NET 2.0

Suppose, we have following app.config file:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="DBConnectionString" connectionString="CONNECT TO SOME SQL SERVER"/>
</connectionStrings>
</configuration>
In new scheme, used in .NET 2.0 we need to use ConfigurationManager class like follows:
System.Configuration.Configuration config =    ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
string stringToConnectoToMyDB = config.ConnectionStrings.ConnectionStrings[1].ToString();

No comments: