1

I am using a direct connection string which is

string ConString = "Data source=.\\Sqlexpress; initial catalog=MyDB; integrated security=SSPI";

I need a connection string that is stored in web.config file like:

<connectionStrings>
<add name="SqlCon" connectionString="Data source=.\\Sqlexpress; initial catalog=MyDB; integrated security=SSPI" providerName="System.Data.SqlClient" />
</connectionStrings>

The problem with this is that it is not accessible in my Data Access Layer. I am working on 3 layer architecture model. I won't like adding references to my Data Access layer, such as System.Web etc. Every day my class libraries get more and more classes. I realize it is time to use a globally accessible connection string so that when I finally submit my project the connection string will need to be changed only at one place on the university computer.

3
  • 3
    a quick internet search would provide many examples Commented Jul 24, 2013 at 0:32
  • 1
    Why is it not accessible from the DA layer? You can use System.Configuration.ConfigurationManager.ConnectionStrings["SqlCon"].ConnectionString Commented Jul 24, 2013 at 0:42
  • I am sorry for saying this, but actually i didn't know that each time you change your layers you have to re-build you solution in order to include most recent changes. Thank you for your support. it is now working Commented Jul 24, 2013 at 0:50

1 Answer 1

1

You won't have to add a reference to web dlls in your data access layer.

It all depends on your structure, but if the dlls are copied to your web sites's bin folder thorough project references - the web config connection string will be used directly from the web site's web config. The connection will be resolved at runtime in the host which would be the web site hosted by some web server. No need to add one for the data access dll itself. This means that for a single process architecture - your connection string is already globally available.

If your data access layer is hosted on an app server, you might have a web config for a web service exposing the business/data layer. A common pattern is to expose your business layer w/data access using a WCF service. The service will then have it's own web.config file - separate from the web.config defined by the web site. The key point though is that only the WFC service needs the connection string. The web site's web config will typically only contain information about how to invoke the service (WCF configuration).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.