ConfigurationSettings in Class Library

  • Thread starter Thread starter Random
  • Start date Start date
R

Random

I'm writing a class library for my web app data access, but don't want to
hard code the database connection string in the class. Nor do I want to
pass the connection string in from the web app every time I access it. I
want to define the connection string in the web app ConfigurationSettings
and let the class library reference it itself whenever it needs a
connection.

How can I do this?
 
Hello ?,

I don't understand why you don't want to put the connection string in the
web.config file. You can use can add a key to the appSettings node and
reference it in code:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="myconnstring" value="connectionstringgoeshere" />

In code:

db.connstring = ConfigurationSettings.AppSettings("myconnstring")

Ken
 
That's precisely where I do want to put it. My class library, however, is
not a part of the web application, but would be a referenced dll that runs
within the HttpContext.
 
Hello Random,

So?

ConfigurationSettings references the config file of the application using
it. This should not be a problem for you. A referenced assembly can use the
ConfigurationSettings of the parent application.
 
Back
Top