Where is the best place to put the connection string

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
 
M

Matt Berther

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

Steve C. Orr [MVP, MCSD]

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")
 
K

Kevin Spencer

I'd put it into my Connection. ;-)

--

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

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

Forum statistics

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

Latest Threads

Top