Moving connection strings to web.config file

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?
 
M

Michael Ramey

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

ConfigurationSettings.AppSettings("ConnectionString")
 
A

Andrew Banks

Appreciated Michael, thanks

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

ConfigurationSettings.AppSettings("ConnectionString")
 
A

Andrew Banks

Actually Michael, I've just tried this and it's saying the Configuration
namespace could not be found

Any ideas?
 
M

Michael Ramey

Perhaps try this,

System.Configuration.ConfigurationSettings.AppSettings("ConnectionString")
 
A

Andrew Banks

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

Thanks for your input
 
W

William Ryan [eMVP]

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
;-)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top