ConfigurationSettings in Class Library

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?
 
K

Ken Cox [Microsoft MVP]

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
 
R

Random

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.
 
M

Matt Berther

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.
 

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,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top