Reputation: 640
I am developing a windows form application using C#. I am showing a grid with values comes from database. My question is how to update grid when changes are made in database by means of any method.
I have tried SqlDependency class. But, my grid gets refresh continuously and i don't need it to refresh. Is there any other way to track changes in database? Is it possible to update data set if any one made changes in database?
Upvotes: 1
Views: 1538
Reputation: 2343
Use an Open-Source class SqlDependencyEx. It is pretty easy to configure and use:
int changesReceived = 0;
using (SqlDependencyEx sqlDependency = new SqlDependencyEx(
TEST_CONNECTION_STRING, TEST_DATABASE_NAME, TEST_TABLE_NAME))
{
sqlDependency.TableChanged += (o, e) => changesReceived++;
sqlDependency.Start();
// Make table changes.
MakeTableInsertDeleteChanges(changesCount);
// Wait a little bit to receive all changes.
Thread.Sleep(1000);
}
Assert.AreEqual(changesCount, changesReceived);
Hope this helps.
Upvotes: 1
Reputation: 4081
You can implement SQLServer notification, instead of polling which you must be doing currently, so it refreshes the grid after a particular interval of time.
using service broker and SqlCacheDependency, you can achieve it, you can go through this article to implemnt it.
Upvotes: 1
Reputation: 35380
Check Query Notifications that were introduced in SQL Server 2005. Query Notifications allow applications to be notified when data has changed.
Upvotes: 2