What is the best place to store a connection string?

L

Leo Muller

I am about to deploy my first ASP.NET project.
But, I was used to store my variables, such as connection strings, in the
global.asa file, in an application. However, these variables are different
on my development and production server. If I use the global.asax file, then
it is in the code behing, compiled part. Thus I can't change the setting on
my production server.

So where is the right place to save my connecting string?

Leo
 
K

Kristof Van Praet

The Web.config file might be a good place. In the system.configuration
namespace, you can find the appropriate methods to access this file in you
application.

Regards,
Kristof
 
S

Sheila Jones

That's what I have done - here's part of my Web.config file:
<configuration>
<appSettings>
<add key="MyConnectionString" value="...the connection string goes
here..." />
</appSettings>

I then retrieve it using
ConfigurationSettings.AppSettings["MyConnectionString"]

I also set the BuildAction property of the Web.config file to None, to stop
it overwriting the version on the live server.

Hope that helps!
 
L

Leo Muller

Hi,

This helped me out, it works fine.

thanks,

Leo

Sheila Jones said:
That's what I have done - here's part of my Web.config file:
<configuration>
<appSettings>
<add key="MyConnectionString" value="...the connection string goes
here..." />
</appSettings>

I then retrieve it using
ConfigurationSettings.AppSettings["MyConnectionString"]

I also set the BuildAction property of the Web.config file to None, to stop
it overwriting the version on the live server.

Hope that helps!


Kristof Van Praet said:
The Web.config file might be a good place. In the system.configuration
namespace, you can find the appropriate methods to access this file in you
application.

Regards,
Kristof

file,
then setting
on
 
J

Josh Harris

Leo, use the web.config as follows:

<configuration>
<!-- application specific settings -->
<appSettings>
<add key="connectionString" value="user id=your
username;password=userpassword;initial catalog=nameofdatabase;data
source=servername;Connect Timeout=30" />
</appSettings>
<system.web>
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top