Connection String

  • Thread starter Thread starter BobLaughland
  • Start date Start date
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.
 
System.Configuration.ConfigurationManager.ConnectionString["LocalSqlServer"].ConnectionString

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

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

or

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