tags:

views:

2583

answers:

6

Hi

I have two projects:

  1. ASP.Net 3.5 website (frontend, UI)
  2. VB Class Library (dataaccess logic)

Where should I save my connectionString, so that I can use if from the class library? And how does this affect where it is placed when I deploy/publish?

Note:

  1. I don't want to pass it to every function in my VB Class
A: 

We keep ours in the machine.config of each server and have a custom DAL to handle all DB interaction for our web apps.

Chuck
I deploy to a hosted environment, so unfortunately no access to machine.config
Kjensen
A: 

Put it in the web.config in the connection strings section.

In the VB project use HttpContext.Current.GetSection to retrieve the section.

AnthonyWJones
Well.. What then what when I want to use the Class Library from a Windows App? There must be a way to make it work for both without sending the connectionstring as a parameter?
Kjensen
@Kjensen: I think you missed my point. You would use HttpContext.Current from inside you VB project there is no need to pass anything around as a parameter.
AnthonyWJones
I did not make myself clear enough. :)The current frontend is a webapp, so I can use httpcontext from inside the vb class library.But if I put a windows-application as a frontend, I can't use httpcontext (since obviously no http-context is available).
Kjensen
@Kjensen: I see, in that case you other suggestion of using Configuration manager will work, albeit less optimal in a Web solution, but that doesn't actually matter when you want a DAL to work for both scenarios (since the DAL is going to be sub-optimal for at least one the scenarios anyway).
AnthonyWJones
+6  A: 

Depending on how you constructed your DAL -- LINQ, TableAdapters, etc. -- it may automatically look for it in the web.config file. If you created the DAL via a designer, likely it stores the default connection string in the app.config file for you class library. I copy the connection strings section from the app.config file to my web.config and change the connection string to the correct database (I have separate web.config's for DEV/QA/PROD). This makes it trivial since the designer generated code already has the code implemented to retrieve it from the configuration file.

If you are hand-coding your DAL and need to pass in the connection string, I suggest setting up a strongly-typed configuration class that interfaces to the web.config and does lazy loading of your configuration values. Use a factory to create your DAL and inject the configuration class into your factory via constructor so that it knows how to create your DAL with the connectionsString retrieved from the configuration file.

tvanfosson
Thank you for your reply. Where can I read more about using a factory? I don't know what that is (blushes).
Kjensen
The classic reference is Design Patterns (http://en.wikipedia.org/wiki/Design_Patterns_(book)) by Erich Gamma, et. al. A factory is basically a specialized class that contains factory methods (see link at reference) that are used to decouple creational code from the classes that use the objects.
tvanfosson
+1. This is the most flexiable solution.
AnthonyWJones
+2  A: 

My question came from having spent half a day of trying to make this work, but I kept getting the wrong connection when deploying (where I use another database).

My problem was, that I was using

My.Settings.DefaultConnectionString

...To retrieve the connectionString in my VB Class Library.

After following tvanfossons anwer, I dug around some more and found out, that I can simply use (after referencing system.configuration) :

System.Configuration.ConfigurationManager.ConnectionStrings.Item("DefaultConnectionString").ConnectionString

It looks in the web-config for webapplications and app.config for windows/class library apps.

I am glad it now works, but even more glad I know why. ;)

Kjensen
A: 

A fellow developers idea once was that we should store all the connection strings in a database table.

Don't try doing that. You won't get very far. :)

Chris
+1  A: 

I had the same issue you were having and I ended up using the System.Configuration.ConfigurationManager class to obtain the connection string stored in my web.config file from my class library like Kjensen's answer suggested. This worked wonders, if I had more experience I would vote that answer up.

I needed the connection string to build my Linq2Sql data context, which this method provided me with.

I now build my data context like below (remembering to add a reference to System.Configuration) -

public MyDataContext() : base(System.Configuration.ConfigurationManager.ConnectionStrings["MyConnectionString"] .ConnectionString, mappingSource)

And as long as the web.config file contains "MyConnectionString" the configuration manager takes care of the rest.

mig
So ConfigurationManager is sort of global in that way that it uses different context depending on the project type. In a webApp - web.config. Is that right?What is usually recommended in these cases, add a reference to ConfigManager or somehow pass the connection string as a parameter in the constructor?
Adam Asham
I'm not sure I understand your question, the ConfigurationManager from what I understand it will read the web.config (or app.config depending on the project type) and provide you with the relevant sections if they exist. Using the ConfigManager to me seems cleaner than passing the string as a constructor (which you have to get from Configuration [or at least SHOULD] anyway), it really depends on what you feel comfortable with I think, I don't see any advantages to not using the ConfigManager approach.
mig