5.15. Updating Data Asynchronously Using Message Queuing
Problem
You need to asynchronously update data in a database on a system that is not always connected.
Solution
Use message queuing and a XML DiffGrams 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 solution performs the following steps:
Creates three message queues, if they do not already exist—one each for the query command (
AdoNet35Cookbook_queueNameUpdateQuery), the query results (AdoNet35Cookbook_queueNameUpdateResult), and the update command (AdoNet35Cookbook_AsynchronousUpdateCommand). In this example, the query and update commands are issued from the same computer that processes the query and update—in a real situation, these tasks would be processed on different computers that might not always be connected.Creates a query message and sends it to the
querycommand queue.Gets and deserializes the query message from the
querycommand queue, creates and executes the query, and sends the result set to theresultqueue.Gets the result set from the
resultqueue and creates the resultDataSetby deserializing the message body.Outputs the result set in the
DataSetto the console.Updates the row in the first (only)
DataSettable withId = 2.Creates a
DiffGramcontaining ...
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