Two-Way Synchronization between Azure SQL and wpf/uwp clients - c#

I have a Azure SQL database, and then clients (UWP and WPF) running SQLite that gets data from the database.
The clients can change the data and it updates the Azure SQL rows and they can insert new items/rows.
My problem is that multiple clients are working at the same time and i would like the client to update as soon as there is a change (insert/update/delete) on the Azure SQL database. Today the user has to press update in order to get latest.
However I not sure how to implement this.
I have been looking into using SQL "Change Tracking" https://learn.microsoft.com/en-us/sql/relational-databases/track-changes/about-change-tracking-sql-server?view=sql-server-2017and can easily active Change Tracking in Azure on the SQL server and Table, however i cannot find any documentation on how to use this to sync to client in C#, is there some?
Maybe there is a better way of doing this?
Any input is much appreciated.

Related

How to sync local data to server in Blazor WebAssembly-PWA?

I want to building a Blazor WebAssembly Progressive Web App, that can run offline.
I began Blazor this morning, and I'm just trying to get the hang of it.
To begin I want to do something like keep.google.com. You can work on you notes offline, on different devices, and when the connection is re-established, all notes are synchronized in the background with the server.
My idea is to have simple notes on a server, with an id, title and a message. These notes can be displayed and added/modified from the client. Since I want the application to work offline, I want the synchronization process to be as follows:
The fist time visiting the website, all notes are fetched from the server,
When notes are added/modified, they are saved on the server,
If connection is lost, notes can still be read and added/modified localy,
When the connection is re-established, the modifications are saved to the server,
Periodically or after pressing a button, sync is done between client and server to fetch new data present on the server.
I think the way to do this is to have a copy of the database localy. Client do modification on the local database and periodically/after pressing a button/when connection is re-established, I sync local database with server database.
I'm sure there is an official and easy solution to do that. I followed the CarChecker example from Microsoft, but they used the IndexedDB in javascript to do that (23min13 in the official tutorial video).
Do you know a .NET solution/tutorial/service that store data locally, and sync in the background with the server ?
I wrote a Blazor WebAssembly PWA with similar technical requirements. There is certainly more than one way to accomplish this but the steps I used are as follows:
I used sqlite on the client side to persitist the data locally. The simplest way to make that persist-able with the ability to use Entity Framework is to use the SqliteWasmHelper nuget package. https://github.com/JeremyLikness/SqliteWasmHelper
On startup and/or when online I fetch the necessary data and insert it into the local sqlite database.
The user can make changes and I save that to the local sqlite DB and mark it as ready to be synced.
I have a background service with a timer which executes on a configurable interval and grabs the local data marked to be synced and calls the API on the server to save the data to a SQL Server database. Of course I check to see that the user is online before attempting the sync.
I use Javascript to determine whether the device is online. I can provide that to you if you need but you should be able to google it.
I have an application update checker based on this method which works pretty well: https://whuysentruit.medium.com/blazor-wasm-pwa-adding-a-new-update-available-notification-d9f65c4ad13
I hope that helps. I'm happy to provide more detail if you like.

How to use Azure SQL database when internet is down?

Let's say I have a C# WPF application connected with Azure SQL database, everything works fine and my queries work well. The only issue is when sometimes the internet is down, it cannot connect with the database and shows an error at the connection string (which is pretty obvious). Is there any way I can alter my connection string to connect with the database offline, at least be able to view the data, given when the internet is back, the updates are sent to the Azure portal as well (preferably automatic but manual will work). Through some research, I was able to get something as Sync my database with others option in the Azure portal. I was wondering if that's what I am looking for or is there any other alternative that you may suggest?
Thanks.
You can take advantage of Azure SQL Data Sync to sync even a local SQL Server Express database with your Azure SQL Database. Your Azure SQL Database will be configured as a "hub" database and the local SQL Server instance as "member database". You can configure automatic sync on/off and who wins (the hub or the member database) in case of conflicts). You need to configure the sync bi-directional so you can connect to Azure when Internet is available manipulate data and connect locally when there is no Internet connection. Once the Internet connection is restored the can be synced as you have configured it, manually or automatically. You will have to install an Agent on the local computer where SQL Server resides. For information about how to configure SQL Data Sync please visit this tutorial.
Take in consideration tables in your database should have a clustered index to be able to use SQL Data Sync.

How to update data from local SQL Server database to online SQL Server database?

I am creating a web site which I want to show data of my local SQL Server database. My local SQL Server database is updated by other desktop application.
I have created an online SQL Server database which is used by my website. I want to update my online database with my local database in every 10 minutes.
How can I do it programatically? Or is there any way to do my task than how can I do it??
I am badly stuck at that. Please help me out.
Thanks in advance....
As M.Ali suggested, SQL Server Replication is your best choice.
It's not hard to implement an SQL Server replication and I'm not going to talk too much details as there are many useful subjects on that matter.
The idea is that the local database is the publisher DB and the online one is the subscriber.(based on your requirements)
If the online one is for read only, then you can create a snapshot or a transnational replication on your local database and you can schedule at the subscriber(online Database) to pull the subscription every 10 minutes.

How to Synchronize SQLServer Database and MySQL Database

My Scenario:
I have two applications. First one is a website which is connected to MySQL Database and 2nd one is a Desktop Application which is connected to SQL Server2008 R2 Database.
The Desktop application updates records locally and the MySQL database is updated online though the website.
Problem:
Two different databases, how can we update at the spot when changes are made either in MySQL or SQL Database?
What I Want:
Databases should be synchronized to each other (e.g. if changes are made in MySQL then SQL server database should be updated, or if changes are made in SQL Database then MySQL database should be updated)
Could anybody please suggest some code, any idea, or any solution to solve this issue?
Make use of Restful API's to Update information from MS SQL server to MYSQL server.
One of the first things I would point out is that complete and perfect syncing is not possible. Unfortunately there will be data types that exist in SQL Server that don't exist in MySQL and vice versa.
But assuming the data types are pretty simple and the schemas are similar, here are some options:
Use a service bus. You can write an application that monitors both database systems and when it sees a change, it pushes an object onto the service bus. Listeners to the service bus will see the objects and write them to the appropriate destination.
Use triggers like Alex suggested. SQL Server can have CLR code execute on a trigger. The CLR code could be some C# that writes directly to MySQL. Takes some setup, but it's possible. I've investigated running a process from a trigger in MySQL and all options are ugly. It's possible, but security is a major concern. The idea is that a record is changed, trigger is fired and an external process is run.
Write an application that constantly looks for "diffs" in tables and moves data back and forth. You'll need to modify all tables to make sure there is support for date/time stamps for each record so you can track when a record has "changed".

Best way to synchronise client database with server database

I have a datalogging application (c#/.net) that logs data to a SQLite database. This database is written to constantly while the application is running. It is also possible for the database to be archived and a new database created once the size of the SQLite database reaches a predefined size.
I'm writing a web application for reporting on the data. My web setup is c#/.Net with a SQL Server. Clients will be able to see their own data gathered online from their instance of my application.
For test purposes, to upload the data to test with I've written a rough and dirty application which basically reads from the SQLite DB and then injects the data into the SQL Server using SQL - I run the application once to populate the SQL Server DB online.
My application is written in c# and is modular so I could add a process that periodically checks the SQLite DB then transfer new data in batches to my SQL Server.
My question is, if I wanted to continually synchronise the client side SQLLite database (s) with my server as the application is datalogging what would the best way of going about this be?
Is there any technology/strategy I should be looking into employing here? Any recommended techniques?
Several options come to mind. You can add a timestamp to each table that you want to copy from and then select rows written after the last update. This is fast and will work if you archive the database and start with an empty one.
You can also journal your updates for each table into an XML string that describes the changes and store that into a new table that is treated as a queue.
You could take a look at the Sync Framework. How complex is the schema that you're looking to sync up & is it only one-way or does data need to come back down?
As a simply solution I'd look at exporting data in some delimited format and then using bcp/BULK INSERT to pull it in to your central server.
Might want to investigate concept of Log Shipping
There exists a open source project on Github also available on Nuget. It is called SyncWinR, it implements the Sync Framework Toolkit to enabled synchronization with WinRT or Windows Phone 8 and SQLite.
You can access the project from https://github.com/Mimetis/SyncWinRT.

Categories

Resources