Moving connection strings to web.config file

  • Thread starter Thread starter Andrew Banks
  • Start date Start date
A

Andrew Banks

I've heard talk of being able to move all sql connection strings to the
web.config file and simply referencing them as opposed to inputting the
string everytime you need to use it.

Can someone give me an example of this please?
 
<configuration>
<appSettings>
<add key="ConnectionString" value="SERVER=sql;DATABASE=myDB;Integrated
Security=SSPI" />
</appSettings>
...
</configuration>

ConfigurationSettings.AppSettings("ConnectionString")
 
Appreciated Michael, thanks

Michael Ramey said:
<configuration>
<appSettings>
<add key="ConnectionString" value="SERVER=sql;DATABASE=myDB;Integrated
Security=SSPI" />
</appSettings>
...
</configuration>

ConfigurationSettings.AppSettings("ConnectionString")
 
Actually Michael, I've just tried this and it's saying the Configuration
namespace could not be found

Any ideas?
 
Perhaps try this,

System.Configuration.ConfigurationSettings.AppSettings("ConnectionString")
 
Ended up with this working
System.Configuration.ConfigurationSettings.AppSettings.Get("ConnectionString
")

Thanks for your input
 
Just a suggestion, but if you use System.Configuration you'll won't have to
fully qualify it (ConfigurationSettings is really long so it's well worth it
;-)
 
Back
Top