views:

126

answers:

2

First, by Remote I mean a central dedicated SQL Server on our Network. By Local I mean a local SQL Express install.

My situation is in house only. No cloud services, external sites, etc. My app allows the user to work disconnected but in order to minimize traffic and a few other issues as well I would like to allow them to connect straight to the central server either automatically whenever it is available and/or when they set a setting choosing Central Server.

Are setup is quite simple. A local connection string for everyone is like so -->

Data Source=.\SQLEXPRESS;Initial Catalog=MemberCenteredPlan;Integrated Security=True

and a Central SQL connection string like so -->

Data Source=CentralSQL;Initial Catalog=MemberCenteredPlan;Integrated Security=True

Also, my Data is in a seperate project from my UI, as such I was having difficulties figuring out how to access the Settings file from the Data layer for the UI layer.

Should I add a parameter to all methods and pass a IsOnline variable to them? Seems repetitive but if I knew a better way I wouldn't be posting in the first place.

Thanks for the help!

This is a very similar post but I wonder if the advice is different when I want to switch between a Local DB and a Remote DB during runtime.

+2  A: 

One option I use is to create a method on the DataContext (in a partial class) in this case:

public static DataContext New
{
  get
  {
    var cs = IsConnected ? CentralConnectionString : LocalConnectionString;
    return new DataContext(cs);
  }
}

You could beef up that switch logic however you wanted for the automatic switching. Then to reference in code, just use a format like this:

var DB = DataContext.New;
var result = from a in DB.....

It keeps your datacontext creation logic in one spot, if it suits your needs, I find it simplifies things everywhere.

Nick Craver
Sounds nice, I think I am missing something though. `IsConnected`...I would set that in the UI, possibly, how would you go about that? I had thought to tie it to the App Settings but I can't seem to access that from the Data layer. Would I be back to passing it to every call?
Refracted Paladin
@Refracted Paladin - You can access it however you want, whether it determines it automatically or does likes James's answer works equally well. You don't need to pass it at all, just get it in a static way. `ConfigurationManager.AppSettings` is the easiest bet if you want an AppSettings approach.
Nick Craver
@Nick Craver: When I first read this I assumed you meant for me to create a partial class for EACH of my Data Contexts (ConnectDataContext, CMODataContext, MCPDataContext), correct? Otherwise, did you mean to have me create a Partial Class for `System.Data.Linq.DataContext`?
Refracted Paladin
@Refracted - You can't create a partial for System.Data.Linq.DataContext, however...you have 2 options. Make a partial for each of your contexts, or create a class that inherits from `System.Data.Linq.DataContext`, add this property, the have your contexts inherit from that class (you can make this change with the LINQ T4 Template for example, you can find those here: http://l2st4.codeplex.com/ ).
Nick Craver
@Nick Craver: Thanks, that is what I figured but I am having issues. Now I know my issues have to do with something else. I'll probably end up posting another question as it looks like I must have done something goofy when I set up my L2S Data Layer....Thanks for the help!
Refracted Paladin
+2  A: 

To answer the question you posed in the comment to Nick's answer, his IsConnected is a property added to the partial class. You can defined it to whatever means to have to determining that state in your program. If it's an AppSetting value, it would be something like this:

public static bool IsConnected 
{ 
  get 
  {
       return ConfigurationManager.AppSettings["Online"] == "true";
  }
}
James Curran
Thank you for answering my comment question. Sorry about that, I will start a new thread as it is actually a User Setting and I cannot seem to access it in the DATA layer with ConfigurationManager. Thanks for the direction.
Refracted Paladin