What is the preferred method for changing connection string parameters in the DAL class library when deploying an asp.net web application?

I deployed an asp.net web application project that has a link to my DAL class library. How do I change the connection string in my DAL after deploying it? The DAL code ignored my web.config connection string and tried to use the app.config value.

I thought I could edit the configuration file associated with the class library, but I could not find it. I temporarily edited the connection string and recompiled and redeployed the library.

Is there a way or way to configure project files where it changes the connection string values ​​based on compilation in debug mode compared to performing compilation with the release. What is the recommended way to work with connection strings in web applications that reference class libraries?

Explanation: DAL library connection strings are also used by some datasets and L2S classes (.dbml), and I'm not sure how to change them to refer to the web.config file that is outside the library in the web application project.

Currently using this code to get around the L2S class problem:

public partial class MyDataContext { partial void OnCreated() { ConnectionStringSettings cs = ConfigurationManager.ConnectionStrings["PrimaryConnectionString"]; if (cs != null) { this.Connection.ConnectionString = cs.ConnectionString; } } } 
+3
c # web-applications deployment connection-string
source share
3 answers

Typically, I allow a top-level project to define this through web.config or app.config; or, specifying that the application should include a connection string with the name "FOO" or (which is much better) that allows the calling application to transmit (one of) a connection key, connection string, or connection to the dll.

Then you basically just edit web.config as usual ...

+2
source share

I usually put the line in the web.config file and let the class (possibly the global class used to reference the settings in web.config) refer to it with the following line:

 class Globals { static string ConnectionString = ConfigurationManager.ConnectionStrings["dbConnection"].ConnectionString; } 

and use it elsewhere, for example:

 IApartmentRepository apartmentRepository = new ApartmentRepository(Globals.ConnectionString); 

It was also a proposal by Saif Khan in his response.

As for modifying connection strings based on compilation mode, Visual Studio 2010 supports this, see Web Deployment: Converting Web.Config from the Visual Web Developer Team Blog. I do not think that Visual Studio 2008 can do this out of the box, however, perhaps it can be done using some kind of build script.

+2
source share

I do not believe that there is a recommended way, but a preferred way. Some connection strings are stored in the web.config file, and some are stored in the registry or machine.config file, and some are stored as a last resort and stored remotely ... yes, I saw that.

The most common repository that I see and use myself is stored in the web.config file. In my DAL objects, I am making a call to the web.config file for the connection string

string connStr = ConfigurationManager.ConnectionStrings ["myString"]. ConnectionString

As for automatically changing the connection string based on a compiled application, I have never seen this. You may need to put this check in the DAL itself to check if the debugging mode is "on" or "off". This will require 2 entries of the connection string in the web.config file.

0
source share

All Articles