Connection String

B

BobLaughland

Hi,

I am writing an ASP .NET 2.0 web site. I have the code below in the
web.config file.

<connectionStrings>
<remove name="LocalSqlServer" />
<add name="LocalSqlServer" connectionString="Data Source=****;Initial
Catalog=****;Persist Security Info=True;User ID=****;Password=****"
providerName="System.Data.SqlClient" />
</connectionStrings>

This works fine for the things on my web site that are grid views and
things like that.

I would like to be able to write my own code to connect to the database
using the connectionString above. I was wondering how programatically I
can grab that connectionString out of the web.config file and use it in
one of my C# code files.

I have it hard coded at the moment in one of my classes, which is bad
if anything to do with the SQL server changes and I forget to change
both places. E.G. once my web site host changed the IP address in my
web config file because they changed SQL servers, but that busted parts
of my site.

Thanks,

Peter.
 
K

Karl Seguin [MVP]

System.Configuration.ConfigurationManager.ConnectionString["LocalSqlServer"].ConnectionString

you need to add a reference the System.Configuration assembly.

Karl
 
C

Christopher Reed

System.Configuration.ConfigurationManager.ConnectionStrings["LocalSqlServer"].ConnectionString

or

System.Web.Configuration.WebConfigurationManager.ConnectionStrings["LocalSqlServer"].ConnectionString
 

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,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top