What I want to do:
Listen to DB insert/ update statements on X tables via a C# external application
On each insert/ update I'd like to be notified
after the notification I'd like to retrieve the inserted/ updated row
How I'm approaching this:
Create a DML trigger for each table, that updates service broker queue on each after insert/ update
After this, I'm not entirely clear on the approach. Mainly, how do I receive the notifications from the service broker queue?
From what I understand SqlDependency
requires a query against which the changes are detected. Thus, do I specify a query against a target queue?
Thanks
SqlDependency is an abstraction that happens to use Service Broker under the hood to deliver the notifications. In this case, I think you can ignore it (it being SqlDependency)
I've actually implemented a similar system (queue up changes in broker). The broad strokes are:
receive top(1000) cast(message_body as xml) from yourQueue
. Make sure you read up on best practices here (things like poison message handling, etc). You could wrap that all in a stored procedure and have your application call that. Also, if you're eschewing activation, you're probably polling at some interval, chances are that when your application does poll, you're going to want to get all of the messages (or at least be notified that there are more). So you'll want to do the receive
statement in a loop or do something clever like "Oh… I asked for at most 1000 and I got 1000. There are probably more!" so you know to go back immediately to process the queued messages. Let me know if you'd like me to elaborate on any of this.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.