views:

77

answers:

2

Hello, I am writing an application that needs to continuously update its displayed data from a view in a database. Currently, it is periodically polling the entire data set and redisplaying it. I am changing it to be more efficient and am interested in people's opinions on how to do this most effectively.

The application is a Windows Forms application and uses a DataGridView to display the data. The database server is SQL Server 2008.

I've been reading about SQL Server change tracking features and am wondering if that might be relevant? Can anyone suggest any good approaches to take here? I would need it to insert, update and delete rows in the application without affecting the state of the current selected row or position of the scroll bars.

Thanks!

+1  A: 

The simplest way is to query by timestamp column. When you first run your app it will query entire dataset and remember the greatest timestamp. Then it will poll with query where timestamp > @currentTimestamp and remember it again.

Andrey
Sometimes this data is deleted too, which I suppose would require a separate table with the primary keys of the deleted rows to be checked also. I also don't really want to continuously poll the database; I think it would be better to have a notification as soon as the data is changed. Thanks for your answer though!
George Xyzzy
@George Xyzzy getting notification is something not from database. you have to use some service oriented approach then, either have message bus of web/wcf services.
Andrey
Thanks that makes sense now
George Xyzzy
A: 

If I were tackling this problem, I'd look into doing it as such:

  1. Create a SQL Server database project
  2. Add a trigger to the project
  3. Have that class send notifications, possibly using MSMQ, to the interested app or
  4. Look into registering an event handler with that SQL Server class... not sure how or if that would work, but raising an event to your app from the trigger would be ideal

I can get more specific if you need, but that's the general approach

Info on triggers in SQL Server projects: http://msdn.microsoft.com/en-us/library/938d9dz2.aspx

James B
Interesting approach - so you are saying that a trigger can run CLR code inside the SQL Server itself? I will have to look into that, thanks!
George Xyzzy
Some links to help you get started:http://msdn.microsoft.com/en-us/library/ms254498%28VS.80%29.aspx(discusses hosting managed CLR in SQL Server)http://msdn.microsoft.com/en-us/library/6s0s2at1%28v=VS.80%29.aspx(Tutorials and examples of what you can do with SQL Server-hosted CLR code)http://richarddingwall.name/2008/09/29/sql-server-2008-database-projects-in-vsts-2008/(You'll need to install this for SQL Server 2008 project templates)HTH!
James B
Also note that CLR integration is disabled by default... to turn it on, read here:http://msdn.microsoft.com/en-us/library/ms254506%28v=VS.80%29.aspx
James B