开发者

I want to execute a code in c# whenever a new row is inserted in the database

i am working on a chat application where the chat message from one client gets into the database and the other clients poll every 5 secs (using a timer) whether there is any new message or not, if yes, the messages are fetched and dis开发者_开发百科played. But this creates a problem when one user is on a slower and connection and the other one is on a very fast one. I want to develop a mechanism where a method gets executed just when a new row is inserted in the database so that the need for polling does not remain. The users will fetch the msgs only when a new chat message enters into the database by any of the clients, otherwise all remain idle. Any help would be highly appreciated. I am using asp.net and C#.


What you are describing is moving from a polling model to a publish/subscribe model (which as you can appreciate is a much better way to do chats/feeds than polling). Two possible resources are this article Publish/Subscribe design pattern implementation in C# and this previous stack overflow question about Pub/Sub architecture.


I'll stay away from commenting on different methods of implementing your application and concentrate on answering the question you asked - you can accomplish this using Query Notifications. There is a lot you can do with these, but the basic premise is that once you have set this up, you give SQL server a query you are interested in, and it will notify your application when the results change.

There's a reasonable tutorial (the code is in VB.NET, but it doesn't really change much) here.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜