Browse DevX
Sign up for e-mail newsletters from DevX


Synchronize Your Databases with .NET Web Services (Part II)

When data gets created in many locations, you often need to create a process that collects and copies this data to multiple sites. In this article, you'll see how to use Web services to automatically synchronize remote databases in a decentralized way, by letting each machine query the others until they all contain the same data.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

n the first part of this article series, you saw how to expose data via an ASP.NET Web service, in an XML format useful for all kinds of clients. By exposing a .NET DataSet object, you effectively deliver a standard format XML document containing a DataSet represented as a diffgram. This diffgram can be used to reconstruct an exact copy of the original DataSet—complete with tables, columns, data types, metadata, primary, and foreign keys, and even table relationships. An Overview of the Synchronization Process
This article expands on the principles discussed in the previous article to build a utility that synchronizes the contents of two or more databases over the Internet, delivering to each the rows that have been added to any other. The principle is relatively simple:

You can assume that:

  • All the tables to be synchronized exist in each database, and those tables have identical structures across all the databases.
  • Each table has a column containing a value that identifies its parent database or machine, so that rows can be identified depending on which machine they originally came from.
  • Each machine exposes the same Web service that can deliver a DataSet containing new rows to any of the other machines for synchronization.
  • Each machine calls this Web service on each of the other machines or a regular basis, fetching new rows from each one and adding them to its own database.
The schematic in Figure 1 shows the overall process for two machines. Note that the process must avoid collecting all the new rows from the other machine(s). By specifying the value for the column that identifies each machine when querying the Web service, we ensure that only the new rows that have been added to that specific machine are returned. Without this restriction, the first machine would copy the new rows from the second machine, and then—when the second machine fetches new rows from the first machine it would also collect the rows it previously sent to the first machine, causing duplication of the new rows.

Thanks for your registration, follow us on our social networks to keep up-to-date