connection string

G

Guest

where is the best place to put the connection string for use through-out my
app?
right now I use, in every page/sub/etc:
SqlConnect = New SqlConnection("Server=xxxx;uid=xxx;pwd=xxxx;database=xxx")
for sql server 2k connection

This connection is for all users,and is the only connection for this app.

maybe in the Global.asax or webconfig file???

thanx!
 
K

Karl Seguin

web.config is the best place:

<appSettings>
<add key="connectionString" value="1" />
</appSettings>

under the <configuration> root element. You can then access it via
System.Configuration.ConfigurationSettings.AppSettings("connectionString")
in vb.Net and with [ ] in c#

Karl
 
G

Guest

The last project I worked on we held the value in web.config but used the
Application_Start event to populate a global variable.

Was this "worth" it? Or should we have just stuck with GetConfiguration
calls each time?

My thinking was get the valule in memory and save the file read each time...

What do you guys think?

(e-mail address removed)
 
G

Guest

In my projects I use an utility class like that :

public sealed class Settings
{
private static string connectionString = "";

public static string ConnectionString
{
get { return connectionString; }
}

static Settings ()
{
ConnectionString = ...AppSettings["connectionString"];
}

}
 
K

Karl Seguin

i agree with vko, depending on what you mean by "global variable" was it
just a static field (good) or using the Application object (bad) ?

Also, not that the web.config is cached internally...so perf won't change
much..more about usability...and I think vko's example provides the greatest
abstraction.

For the record, I only ever use custom configurations, so that's how I do
it:
http://openmymind.net/Configuration/index.html

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/


Vko said:
In my projects I use an utility class like that :

public sealed class Settings
{
private static string connectionString = "";

public static string ConnectionString
{
get { return connectionString; }
}

static Settings ()
{
ConnectionString = ...AppSettings["connectionString"];
}

}

alaspin said:
The last project I worked on we held the value in web.config but used the
Application_Start event to populate a global variable.

Was this "worth" it? Or should we have just stuck with GetConfiguration
calls each time?

My thinking was get the valule in memory and save the file read each time...

What do you guys think?

(e-mail address removed)
 
G

Guest

Yes, we lobbed it into the Application object (old ASP habit)

Good point about the cacheing - thnaks guys

(e-mail address removed)

Karl Seguin said:
i agree with vko, depending on what you mean by "global variable" was it
just a static field (good) or using the Application object (bad) ?

Also, not that the web.config is cached internally...so perf won't change
much..more about usability...and I think vko's example provides the greatest
abstraction.

For the record, I only ever use custom configurations, so that's how I do
it:
http://openmymind.net/Configuration/index.html

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/


Vko said:
In my projects I use an utility class like that :

public sealed class Settings
{
private static string connectionString = "";

public static string ConnectionString
{
get { return connectionString; }
}

static Settings ()
{
ConnectionString = ...AppSettings["connectionString"];
}

}

alaspin said:
The last project I worked on we held the value in web.config but used the
Application_Start event to populate a global variable.

Was this "worth" it? Or should we have just stuck with GetConfiguration
calls each time?

My thinking was get the valule in memory and save the file read each time...

What do you guys think?

(e-mail address removed)

:

where is the best place to put the connection string for use through-out my
app?
right now I use, in every page/sub/etc:
SqlConnect = New SqlConnection("Server=xxxx;uid=xxx;pwd=xxxx;database=xxx")
for sql server 2k connection

This connection is for all users,and is the only connection for this app.

maybe in the Global.asax or webconfig file???

thanx!
 
J

Juan T. Llibre

When you store it in web.config, it *is*
in memory after the application starts.

web.config is loaded into memory
as soon Application_OnStart initializes.



Juan T. Llibre
ASP.NET MVP
===========
 
G

Guest

thanx guys, this did the trick. I also added this to my custom dll that I add
to my project to control login. Works like a charm.

Karl Seguin said:
web.config is the best place:

<appSettings>
<add key="connectionString" value="1" />
</appSettings>

under the <configuration> root element. You can then access it via
System.Configuration.ConfigurationSettings.AppSettings("connectionString")
in vb.Net and with [ ] in c#

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/


Chris said:
where is the best place to put the connection string for use through-out my
app?
right now I use, in every page/sub/etc:
SqlConnect = New SqlConnection("Server=xxxx;uid=xxx;pwd=xxxx;database=xxx")
for sql server 2k connection

This connection is for all users,and is the only connection for this app.

maybe in the Global.asax or webconfig file???

thanx!
 

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,778
Messages
2,569,605
Members
45,237
Latest member
AvivMNS

Latest Threads

Top