views:

397

answers:

3

I originally wrote a data access layer inside an App_Code folder in my website. Then we developed a web service in a separate project. So that both the website and the web service can access the same DAL, I moved it to a different project. I have a dataset with tableadapters and to make the DAL project compile I had to add the connection string to the Application property settings. But this means that I have to recompile the DAL for every deployment. Also I may have 2 or 3 websites on the server using the same DAL. So I want to set the connection string in each website's web.config and leave it at that. Do I have to go through my code and change every time i create an instance of a tableadapter? e.g. From

using (MessageQueue adaptor = new MessageQueue())
{
    return adaptor.GetMessages(UserId, MobileId, StartDate, EndDate);
}

to

using (MessageQueue adaptor = new MessageQueue())
using (OracleConnection connection = new OracleConnection(OracleUtilities.ConnectionString))
{
    adaptor.Connection = connection;
    return adaptor.GetMessages(UserId, MobileId, StartDate, EndDate);
}

Or is there a better way?

Colin

+1  A: 

Use a standard ConnectionStrings configuration section and you will be able to retrieve the connection string without resorting to a utilities class:

System.Configuration.ConfigurationManager.ConnectionStrings

You will still have to put the connection string into the app.config or web.config file of each application that requires the database.

freshr
The utilities class works. It uses ConfigurationManager internally and correctly retrieves the connection string from web.config. But the Dataset designer requires an application setting, and the code doesn't currently use the utility. I think I will have to dynamically set every tableadapters connection. Or is there a better way?
Colin
I thought the DataSet designer used this ConnectionStrings section directly. But if not, perhaps you could use a Factory pattern to return correctly configured adapters... That way at least the dynamic setting of table adapters is all done in the one place.
freshr
I've found it does if the names match - but the names have to be fully qualified - see the more detailed answer I've added. So no need for dynamic setting of table adapters!
Colin
A: 

First of all you have to add reference in your library of System.Configuration in .Net tab from reference window
then open your settings.Designer.cs file, you will look like this..

public string yourConname_ConnectionString {
        get {
            return ((string)(this["yourConname_ConnectionString"]));
        }
    }

Replace this with....

public string yourConName_ConnectionString {
        get {
            return ((string)(System.Configuration.ConfigurationManager.ConnectionStrings["yourwebconfigConName_ConnectionString"].ToString()));
        }
Muhammad Akhtar
Thanks Muhammed. I can see how this will work. The problem with this is that if any developer right clicks on properties and adds or edits a setting, then this code will be overwritten won't it? Any way around that?
Colin
+2  A: 

I found the answer I was looking for here:

http://stackoverflow.com/questions/899346/guidance-needed-asp-net-app-connection-string

in the answer by s_ruchit.

The connection string names in the web.config have to match the connection string name in the app.config - which contains a fully qualified name. So in my case I change

<add name="ConnectionStringMainDB" connectionString="<myConnStringHere>" providerName="System.Data.OracleClient"/>

to:

<add name="DatabaseAccess.Properties.Settings.ConnectionStringMainDB" connectionString="<myConnStringHere>" providerName="System.Data.OracleClient"/>

No other code changes required! Thanks to Muhammed and freshr too

Colin
we have the same problem.. how we resolve problem is... we add custom class that get connection string from web.config and then manually open connection when using tableadapter
Muhammad Akhtar
In the nex project this thing will very help full.
Muhammad Akhtar
When you are using this technique.. Can you plz tell me how you handle transaction when you performing inertion in multiple tables.
Muhammad Akhtar
I've found transactions with TableAdapters to be a real problem. I tried the approach here:http://weblogs.asp.net/ryanw/archive/2006/03/30/441529.aspxBut that broke for some reason when I moved the DAL into a separate project and also I think it may have been a source of memory leaks. I don't understand the pros and cons of the TransactionScope thing, so I wrote a stored procedure to encapsulate my transaction and added that method to the TableAdapter
Colin
I will email you, how we are acheiving transaction... ur email?
Muhammad Akhtar
I think you should ask this as another question on SO. I was looking for the answer to this before and I didn't understand all the implications. There is the link I gave you before that uses reflection to get around the problem, but I had problems with it - that could have been my implementation and I didn't persevere with it.
Colin
Another way is to use partial classes, to get to TableAdapter command objects. I played with that for 5 minutes and gave up, so I created a stored procedure in the database that did everything inside a transaction then added the sql to call the stored procedure to the TableAdapter in the DataSet designer.
Colin
And then again there is TransactionScopehttp://stackoverflow.com/questions/677563/transactionscope-or-idbtransactionI tried this too but he compiler said I needed another oracle client dll or something and that scared me off
Colin