I typically work on an application using about three different computers. It works great except that I was frequently forgetting to change the Web.Config connection string settings after the transfer. Needless to say, the application would crash, I would change the settings and then everything was fine.
To overcome my problem, I started creating a CDb.cs class file in each application where I would place, among other things two static functions.
public static string thisMachine
{
get { return HttpContext.Current.Server.MachineName.ToString(); }
}
public static string DsnCodeMaker
{
get{
string m_dbConnection
= ConfigurationSettings.AppSettings[thisMachine+"_"+"DSN"];
if (m_dbConnection==null)
throw new Exception("DSN not set in Config.web");
return m_dbConnection;
}
}
The first function merely grabs the machine name and returns it as a string. The second function reads the DSN setting from the Web.Config file. Note that the second function attaches the machine name to "DSN" before it makes the call to AppSettings. Assuming the names of your computers were M1 and M2, the connection strings would be set up in web.config as:
<appSettings>
<add key="M1_DSN" value="server=YourServer1;uid=sa;pwd=;database= myDb"/>
<add key="M2_DSN" value="server=YourServer2;uid=sa;pwd=;database= myDb"/>
</appSettings>
Using this system, I can freely copy between several machines without having to worry about changing the connection strings. If anyone has any suggestions for a better alternative, please let me know.
Another method - You can also "just not copy" the web.config file by right-clicking on the web.config file, select Properties, then under Build Actions, select None - thanks to Remas Wojciechowski for the tip!
Actually I thought about using the no copy approach and decided against it simply because this method keeps the site identical where ever it might be. For most sites, it simply means having three lines is web.config rather than one.
This is where coding becomes like BBQ, there is no best way, only the way that that works best for you!
Top 