Click here to Skip to main content
16,021,181 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello Folks i'm here again with a new existential doubt, i want to my application developed in C# with Visual Studio 2010 and ADO.NET Entity Data Model, be independent of the Data Base Server connection.
What i want to say is that based on the reality, the application must be deployed on differents environments, so in my particular case using as example the environments of my Office and my Home. I must compile a version for each one because the access route for the server in both cases vary and the security too: integrated on one and protected by Username and Password on other.
How can i configure that on a external way? Maybe in the first run of the application, but how i set these properties?.

EDIT: I'm working with Win Forms not Web Forms.

Thanks in advance and sorry for my english.
Best Regards.
Posted
Updated 15-Dec-10 16:55pm
v2

I think you can solve this by using different web.Config files for different servers.
Or
use environment setting based on url supplied and pass connection string as per that.
 
Share this answer
 
Comments
Pablinff 15-Dec-10 22:57pm    
Sorry the missunderstanding it's Winforms not Web Forms.
Thanks anyway!!
Pablinff we've run into the same problem at my work, only not between home and work but between our dev (local) machines and our production servers.

In the end we have gone to the solution of making a common library that gets settings from the web.config file based off the machine the application is running on.

As an example, if I was to connect to the report server I would say

String reportServerName = settings.setting("reportServer")

And in the web.config we would have the following appSettings settings

<add key="dev-reportServer" value="http://developmentReportServerName">

<add key="prod-reportServer">
value="http://productionReportServerName" />

Reply back if you would like more information about how we determine the server/machine that the application is running on and then use that to pick the app setting.

Unfortunately we're using VB.Net so you might have to suffer my answer in that :P
 
Share this answer
 
Comments
Pablinff 15-Dec-10 22:57pm    
Sorry the missunderstanding it's Winforms not Web Forms.
Thanks anyway!!
tenletters 15-Dec-10 23:03pm    
In that case I would do something similar to find the machine name it's deployed to. Then you could just match the machine name to a value and grab the connection string from an xml file. Or if you only have a few options that shouldn't change you could do a switch statement in your code. I recommend the xml file.

Theory is pretty much the same, just all the project components from the web application aren't available to you.
Pablinff 15-Dec-10 23:23pm    
Thanks tenletters, can you explain a little more how to do that with a xml file? and how to detect the machine name and if it's possible the name of the SQL Server Instance.
tenletters 16-Dec-10 0:02am    
So, I am going to assume you're using C# for this.
Create a common class so your application can reuse it.

At the top of the .cs add in:
using System.Security.Principal;

Then you'll want to grab the machine name with the following line:
WindowsIdentity.GetCurrent().Name.ToString();

This will return a string in the format computername/username. Just do a split and take the first part of the array to get the computername.

Then you'd set a XML file in the following way:
<connectionstrings>
<connectionstring id="homePCName" value="your connection string here">
<connectionstring id="officePCName" value="your other connection string here">


Just call it connections.xml or something similar.

Then you open your xml document later in the code with:
XmlDocument xmlConnectionsDocument = new XmlDocument();
xmlConnectionsDocument.Load(Server.MapPath("/xml/connections.xml"));

Hope that helps, I'm converting my code from VB.Net so it might not work perfectly.
Pablinff 16-Dec-10 9:56am    
I'm sorry but the example of the xml file can not be seen.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900