We have implemented crud operation in HTML5 and IndexedDB database to provide offline support. The next step will be the synchronization of offline and online databases. In this article, we are going to implement IndexedDB and SQL Server database synchronization using ASP.NET web API.
To synchronize a local database with a server database:
$('#btnSyncLocal').click(function () { $.ajax({ url: 'api/service?revision=' + localStorage.customerRevision, type: 'GET', dataType: 'json', success: function (data) { if (data.Revision == localStorage.customerRevision) { alert('You are already working on the latest version.'); } else { syncData(data); } } }); });
Server database synchronization with local database:
$('#btnSyncServer').click(function () { var customers = []; db.linq.from(config.objectStoreName).select().then(function () { if (customers.length > 0) { var postData = { revision: parseInt(localStorage.customerRevision, 10), appID: config.appID, customers: customers }; $.ajax({ url: 'api/service', type: 'POST', dataType: 'json', contentType: "application/json", data: JSON.stringify(postData), success: function (data) { if (data.Revision == localStorage.customerRevision) { alert('There is newer version on the server. Please Sync from server first.'); } else { syncData(data); } } }); } else { alert('There is no change in data after your last synchronization.'); } }, handleError, function (data) { if (data.Revision == -1) { customers.push(data); } }); });
For a complete implementation of this example, refer to this link:
Implementation example: Autonomous database synchronization (HTML5 IndexedDB) with an online database using Asp.Net web API
source share