在数据库中的一些数据的变化。我如何能引发一些C#code在做这些改变一些工作? [英] Some data changes in the database. How can I trigger some C# code doing some work upon these changes?

查看:242
本文介绍了在数据库中的一些数据的变化。我如何能引发一些C#code在做这些改变一些工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一些应用程序中的一个数据库。现在我想添加其他应用程序B,应保持应用程序A应用程序B的数据库的变化轨迹应该做一些计算,当数据发生了变化。有两个应用程序之间没有直接的沟通。两者只能看到该数据库。

Suppose I have some application A with a database. Now I want to add another application B, which should keep track of the database changes of application A. Application B should do some calculations, when data has changed. There is no direct communication between both applications. Both can only see the database.

基本的问题是:在数据库中的一些数据的变化。我如何能引发一些C#code根据这些变化做一些工作?

The basic problem is: Some data changes in the database. How can I trigger some C# code doing some work upon these changes?


要给予一定的刺激答案,我提一些方法,我目前考虑:

To give some stimulus for answers, I mention some approaches, which I am currently considering:

  1. 请应用B轮询 变化感兴趣的表中。 优点:简单的方法。 缺点:大量的流量, 特别是很多表都是 参与其中。
  2. 在介绍触发器,它会火 某些事件。当他们开火 他们应该写一些入门到 事件表。只有应用程序B 需要查询的事件表。 优点:较少的流量。 缺点:逻辑被置入 数据库中的触发器的形式。 (这不是的一个问题 触发器邪恶。这是一个设计 的问题,这使得它 缺点。)
  3. 摆脱轮询方法和 使用的SqlDependency类来获得 通知更改。优点: (也许?)的流量比轮询少 做法。缺点:不数据库 独立。 (我知道 OracleDependency在ODP.NET,但什么 对其他数据库?)
  1. Make application B polling for changes in the tables of interest. Advantage: Simple approach. Disadvantage: Lots of traffic, especially when many tables are involved.
  2. Introduce triggers, which will fire on certain events. When they fire they should write some entry into an "event table". Application B only needs to poll that "event table". Advantage: Less traffic. Disadvantage: Logic is placed into the database in the form of triggers. (It’s not a question of the "evilness" of triggers. It’s a design question, which makes it a disadvantage.)
  3. Get rid of the polling approach and use SqlDependency class to get notified for changes. Advantage: (Maybe?) Less traffic than polling approach. Disadvantage: Not database independent. (I am aware of OracleDependency in ODP.NET, but what about the other databases?)

什么方法更有利?也许我错过了一些重要的(DIS)的优势,在提到的办法是什么?也许还有一些其他的方法我还没有想到什么?

What approach is more favorable? Maybe I have missed some major (dis)advantage in the mentioned approaches? Maybe there are some other approaches I haven’t think of?


编辑1:数据库是独立的......让我们把他们......销售人员的一个因素。我可以使用的SqlDependency或OracleDependency。对于DB2或其他数据库,我可以回退到轮询方法。这只是一个问题的成本和收益,我想至少要想想这样我就可以讨论这个问题。

Edit 1: Database independency is a factor for the ... let's call them ... "sales people". I can use SqlDependency or OracleDependency. For DB2 or other databases I can fall back to the polling approach. It's just a question of cost and benefit, which I want to at least to think about so I can discuss it.

推荐答案

我会去与#1。这实际上不是你想象的那样多的流量。如果你的数据不经常变化,你可以悲观,并只取东西,给你一个是还是不是关于表的变化。

I'd go with #1. It's not actually as much traffic as you might think. If your data doesn't change frequently, you can be pessimistic about it and only fetch something that gives you a yay or nay about table changes.

如果你设计的架构与投票记住,你可能没有真正承担那么多的每调查一炮而红。

If you design your schema with polling in mind you may not really incur that much of a hit per poll.

  • 如果你只是添加记录,不改变它们,然后检查最高的ID可能足以在一个特定的表。

  • If you're only adding records, not changing them, then checking the highest id might be enough on a particular table.

如果您要更新所有这些,那么你可以存储时间戳列和索引,然后寻找最大的时间戳。

If you're updating them all then you can store a timestamp column and index it, then look for the maximum timestamp.

你可以发送轮询多个talbes(有效),并返​​回更改表的列表中ubber查询。

And you can send an ubber query that polls multiple talbes (efficiently) and returns the list of changed tables.

在这个答案没有什么特别聪明,我只是想表明,#1可能不是那么糟糕,因为它一开始看起来。

Nothing in this answer is particularly clever, I'm just trying to show that #1 may not be quite as bad as it at first seems.

这篇关于在数据库中的一些数据的变化。我如何能引发一些C#code在做这些改变一些工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆