Click here to Skip to main content
15,893,487 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi Folks, i have a question:
Scenario:
I have developed an application in VS 2010 with C# and i have a data model that points to a database on a specific server. An specific scenario is my Office and my Home: In the Office the Server's name is FS-OFFICE\SQLSERVER and in my home is SERVER\SQLEXPRESS
The databases are the same in both cases.

What i want to do is, depending on the place of implementation of the software, be able of select the server programatically or with a config file instead, because i haven't the possibility to change the servers names.
That is possible?

Thanks in advance.
Best Regards!

SOLUTION: Thanks to Henry Minute changing the target server on the app.config file works great for me. Just changing the string in "Data Source" works.
XML
<connectionStrings>
<add name="FFPFrameWorkEntities" connectionString="metadata=res://*/Entities.FFP_FrameWork.csdl|res://*/Entities.FFP_FrameWork.ssdl|res://*/Entities.FFP_FrameWork.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=FS-OFFICE\sqlexpress;Initial Catalog=FFPFrameWork;Integrated Security=True;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient" />
</connectionStrings>


Thanks Mod! I also learned how to present the code!
Posted
Updated 13-Dec-10 12:33pm
v5
Comments
[no name] 12-Dec-10 21:20pm    
Please elaborate on the solution. An edmx file uses a connection string such as
connectionString="metadata=res://*/EFEntities.csdl|res://*/EFEntities.ssdl|res://*/EFEntities.msl;provider=System.Data.SqlClient;provider connection string="Data Source=.;Initial Catalog=TimingTest;Integrated Security=True;MultipleActiveResultSets=True""
I'm curious how you applied Henry's solution to you problem?

You can have more than one connection string in the config file:
XML
<connectionStrings>
  <add name="Home" connectionString="Data Source=SERVER\SQLEXPRESS;AttachDbFilename=D:\data\MyStuff.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True"
    providerName="System.Data.SqlClient" />
  <add name="Office" connectionString="Data Source=FS-OFFICE\SQLSERVER;AttachDbFilename=H:\data\MyStuff.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True"
    providerName="System.Data.SqlClient" />
</connectionStrings>


and access it something like:
C#
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Home"].ConnectionString);


OR
C#
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Office"].ConnectionString);


Hope this helps. :)
 
Share this answer
 
Comments
MCY 12-Dec-10 17:53pm    
good one
When building and Entity Data Model with Visual Studio the connection string is already stored in the app.config. Browser the code for this article, Entity Framework Performance[^] and you will see the app.config file with the connection strings.

Also, one of the constructors for the DataContext class takes an IDbConnection, so you could create your own and pass it to the ctor.
 
Share this answer
 

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