Visual Studio 2005 (2.0) differs slightly from 2003 (1.1) in the way you can get ConnectionStrings
from the Config file, although the same code that works in 2003 will still work in 2005.
The config file in 1.1 might look something like this:
<?xml version="1.0" standalone="yes"?>
<configuration>
<appSettings>
<add key="ConnectionString"
value="server=MyServer;database=MyDataBase;uid=UserName;pwd=MyPassword" />
</appSettings>
</configuration>
In 2.0, connectionStrings have their own section under the appSettings:
<?xml version="1.0" standalone="yes"?>
<configuration>
<appSettings/>
<connectionStrings>
<add name="WebShopper"
connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=WebShopper;
Integrated Security=True"
providerName="System.Data.SqlClient"/>
<add name="LocalSqlServer"
connectionString="Data Source=.\SQLEXPRESS;initial catalog=WebSite;
integrated security=SSPI;persist security info=False;workstation id=WTI01;
packet size=4096" providerName="System.Data.SqlClient"/>
</connectionStrings>
</appSettings/>
</configuration>
In 1.1, you get the ConnectionString using the ConfigurationSettings class:
string cConnection = ConfigurationSettings.AppSettings["ConnectionString"];
In 2.0, there is a new class, ConfigurationManager, to get the ConnectionStrings:
string ConnectionString =
ConfigurationManager.ConnectionStrings["LocalSqlServer"].ConnectionString;
Note that the 1.1 ConfigurationSettings class still works just fine in 2.0.
from a solution provided by Michael McLain and Bonnie Berent in Message #1071823 and Message #989055
|