Search code examples
asp.net-mvc-3web-configconnection-stringdata-access-layer

Passing the connection string from 2 web applications sharing a DAL?


I have a solution with two MVC 3 application sharing the same DAL libaray. Each application is pointing to a different db and their connection string is stored in their respective web.config file.

What would be the best approach to pass their connection string to the DAL?

From MVC controller:

string _connectionString = WebConfigurationManager.ConnectionStrings["NexGenContext"].ToString();
// Changes???
QuestionDAL qd = new QuestionDAL();
var agency = qd.SearchAgencies(ori, name)

DAL code:

public IEnumerable<AgencyTerm> SearchAgencies(string ori, string name)
        {
            log.Debug("Executing: SearchAgencies(string ori, string name)");
            List<AgencyTerm> agencies = new List<AgencyTerm>();
            using (var conn = new SqlConnection(_connectionString))
            {
                var com = new SqlCommand();
                com.Connection = conn;
                com.CommandType = CommandType.StoredProcedure;
                ... etc....

Solution

  • //Add constructor

    public QuestionDAL(string connectionString)
            {
                _connectionString = connectionString;
            }