I\'m trying to figure out how to use SQL Dependency (C# 4.0) to \'listen\' for changes to a database. I\'ve seen quite a few things on the web, but they seem to be tailored
You can hook up the SqlDependency.Change event and do whatever you like in this event handler. This is, in fact, the only way to do what you want, and there is nothing wrong with it.
In pseudo-code it looks like this:
var dep = new SqlDependency(GetSqlQueryForMonitoring());
dep.Change += () => {
var data = ExecSql(GetDataQuerySql());
UpdateCache(data);
};
Very simple. Just use two different queries.
Edit: In your sample code there is a comment saying you are running on the UI thread. Why should that be the case? I doubt it. Anyway, you should run your query before you resetup the dependency because otherwise you will have the potential for concurrent invalidations happening.
I suggest you get your fresh data from the database and then send a message to the ui to update it (Invoke).