¿Cómo trabajar con dos bases de datos que tienen las mismas tablas en C#?

I'm writing a program & I want to have 2 SQL Server databases. One on the client and one on the server. I would like to check the internet connectivity in my program and if the user is connected to the internet the server database is used and if it's offline then the client database is used and then when the user connects to the internet the server database updates itself with the client database.

As the two databases are exactly the same and have the same tables how can I do this? I mean connection strings and the dbml file contents.

preguntado el 28 de agosto de 12 a las 10:08

2 Respuestas

Since you've mentioned dbml, I've assumed LINQ2SQL.

I would suggest you add 2 connection strings, one for client, one for server.

Entonces usa el DataContext() constructor which takes the connection string name to determine which database the context points to. e.g. aquí

Editar

I would suggest a helper method / factory something like this:

public static MyDataClassesContext GetDataContext(bool isInternetAvailable)
{
  if (isInternetAvailable)
  {
    return new MyDataClassesContext("ServerConnStringName");
  }
  else
  {
    return new MyDataClassesContext("LocalConnStringName");
  }
}

And in your code using DataContexts:

    // Whatever your mechanism is for determining internet availability 
    // (Note that you probably want to cache this bool lol)
    bool isInternetAvailable = GetPingToServer() < 10000 ? true : false;
    using (var dc = GetDataContext(isInternetAvailable))
    {
        ...
    }

contestado el 23 de mayo de 17 a las 13:05

Yes I'm using LINQ2SQL! So you mean I should do it for every function?! Can I just determine which connection string is now being used and then use just one datacontext? - Payam Sh

I'm really thankful but how should I add tables to my dbml file? Cuz I can't drag & drop the tables of the two databases in one dbml file as the tables have the same names. - Payam Sh

Deberías echar un vistazo al Marco de sincronización de Microsoft. It says from itself:

Una plataforma de sincronización integral que permite la colaboración y el acceso fuera de línea para aplicaciones, servicios y dispositivos con soporte para cualquier tipo de datos, cualquier almacenamiento de datos, cualquier protocolo de transferencia y cualquier topología de red.

And it has everything prepared to cover exact your scenario.

Respondido 28 ago 12, 10:08

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.