4.13. Updating Data Asynchronously Using Message Queuing
Problem
You need to asynchronously update data on a database on system that is not always connected.
Solution
You must use message queuing and an XML DiffGram
to:
Construct and send an MSMQ message containing a
DiffGramto the server.Extract the
DiffGramfrom the message and use it to update the data source at the server.Construct and send a message containing the latest
DataSetto the client.Retrieve the response at the client and deserialize it into a
DataSet.
The sample code contains four event handlers:
Form.LoadSets up the sample by loading a
DataSetwith the Customers table from the Northwind database. The default view of the table is bound to a data grid on the form.- Send Update
Button.Click Checks if the
updatemessage queue exists and creates it if necessary. AMessageQueueobject is created to access the queue. A message is created containing aDiffGramof the changes made to theDataSetcontaining the Customers table. This message is sent to the update queue.- Process Update
Button.Click Checks if the
updatemessage queue exists and creates it if necessary. AMessageQueueobject is created to access the queue. An attempt is made to receive a message from the queue, waiting one second before giving up. If a message is received, aDataAdapterthat uses aCommandBuilderto generate updating logic is created and used to update the database with the changes in theDataSetcontained in the body of the message. The latest version ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access