简体   繁体   English

使用C#在SQL Server中监视表

[英]Watching a table in sql server using c#

I need to know if a new record has been added to a particular table in my windows application. 我需要知道新记录是否已添加到Windows应用程序中的特定表中。 Table might be manipulated by different applications. 表可能由不同的应用程序操纵。

For now I'm using a Timer control and querying to see if there is a new record (I add record content somewhere in my application and delete record to avoid getting duplicate record), but of course this is not a clean way for this purpose. 现在,我正在使用Timer控件并查询是否有新记录(我在应用程序中的某处添加了记录内容,并删除了记录以避免重复的记录),但是,这当然不是一种干净的方法。

Is there something like an event or something better than my approach? 有什么事情比我的方法更好吗?

I'm using entity framework 6 我正在使用实体框架6

Update : I've read about SqlDependency but I don't know if it can be implemented using entity framework 更新 :我已经阅读了有关SqlDependency的信息,但我不知道它是否可以使用实体框架来实现

您可以尝试Linq2Cache尝试利用SqlDependency,这是我上次听说EF6已清理其行为 ,现在制定了与Query Notifications兼容的查询

Here's an existing StackOverflow article about that using the SQLDependency class . 这是有关使用SQLDependency类的现有StackOverflow文章。

Alternatively, you can cache the last record ID in memory/local to your program (in a file) or write the last processed record ID to a database table and search for anything more recent than that. 或者,您可以将最后一个记录ID缓存在程序的本地内存中(在文件中)或将最后处理的记录ID写入数据库表,然后搜索比此最近的任何内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM