Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There's a number of different ways of dealing with this.</p> <p>Some people have complete separate copies of their web.config files (where the connection string(s) should be kept) and using post-build events, they can copy a relevant web.config file based upon the "release configuration" you're using in Visual Studio.</p> <p>This method has been <a href="http://weblogs.asp.net/scottgu/archive/2007/09/21/tip-trick-automating-dev-qa-staging-and-production-web-config-settings-with-vs-2005.aspx" rel="nofollow noreferrer">blogged about</a> by Scott Gu, and also <a href="http://social.msdn.microsoft.com/Forums/en-US/msbuild/thread/e9943a75-f129-4c01-b05c-b055eb8e8f13" rel="nofollow noreferrer">others</a>.<br> Also, see this StackOverflow question:<br> <a href="https://stackoverflow.com/questions/180470/how-do-you-deal-with-connection-strings-when-deploying-an-asp-net-site">How do you deal with connection strings when deploying an ASP.NET site?</a></p> <p>Now, the above is are great methods when initially <em>deploying</em> your ASP.NET application, however, if you want to do this when the application is <em>already deployed</em>, you can use this handy mechanism: <a href="http://weblogs.sqlteam.com/travisl/archive/2003/11/18/550.aspx" rel="nofollow noreferrer">ConnectionString Trick</a></p> <p>It does sound like you're doing something very similar to this already, and if you want to be able to dynamically switch databases at runtime, I think the best you can do is to just "switch" connection strings within your code. Of course, the trick here is to simplify this so that only one place in your application is responsible for providing a database connection string to the rest of your application.</p> <p>Probably the best way to handle this is to ensure that all values you wish to retrieve from the web.config are retrieved using a class that derives from the <a href="http://msdn.microsoft.com/en-us/library/system.configuration.configurationsection.aspx" rel="nofollow noreferrer">System.Configuration.ConfigurationSection</a> class. This will allow you to encapsulate any logic (such as the "connection string-based-on-machine-name" trick above) required to "switch" the actual value returned in one place, and allow you to use your custom class's "connectionstring" property in any other component's constructor that requires a connectionstring as a parameter, without having to change this when the actual connection string value changes.</p> <p>Some good resources for custom Configuration Sections are:</p> <p><a href="http://msdn.microsoft.com/en-us/library/2tw134k3.aspx" rel="nofollow noreferrer">How to: Create Custom Configuration Sections Using ConfigurationSection</a><br> <a href="http://aspnet.4guysfromrolla.com/articles/032807-1.aspx" rel="nofollow noreferrer">Creating Custom Configuration Sections in Web.config Using .NET 2.0's Configuration API</a><br> <a href="http://haacked.com/archive/2007/03/12/custom-configuration-sections-in-3-easy-steps.aspx" rel="nofollow noreferrer">Custom Configuration Sections in 3 Easy Steps</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload