Where is the best place to put the connection string

  • Thread starter Thread starter Fernando Lopes
  • Start date Start date
F

Fernando Lopes

Hi there.
In your opinion, where is the best place to put the connection string of a
web application?
Web.config, a constant into the code?

I'm not using a component server, so it's not an option, ok?

Tks.

Fernando
 
Hello Fernando,

web.config.

Constants are to be discouraged for a dynamic piece of information such as
this. You dont want to recompile your application every time you change the
connection string, do you?
 
In the overwhelming majority of cases the best place to put the connection
string(s) is in the web.config file.
Here's what the appSettings section of your web.config would look like:

<configuration>
<appSettings>
<add key="ConnectionString"
value="Server=(local);Database=DBName;UID=sa;PWD="/>
</appSettings>
</configuration>

This is a nice way to manage it. You can change the connection string
easily without rebuilding the app or restarting IIS or anything, and the
change goes into effect immediately.

Then in your code behinds you can get the connection string like this:
Dim sConn as string
sConn = ConfigurationSettings.AppSettings("ConnectionString")
 
I'd put it into my Connection. ;-)

--

Kevin Spencer
Microsoft MVP
..Net Developer
What You Seek Is What You Get.
 
Back
Top