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

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

问题描述

假设我有一个应用程序A与数据库。现在我想添加另一个应用程序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#代码对这些更改进行一些工作?

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只有
    需要轮询事件表。
    优势:交通更少。
    缺点:逻辑以触发器的形式放置到
    数据库中。
    (这不是触发器
    evilness的问题,这是一个设计
    的问题,这使得它成为
    的缺点。)

  3. 除去轮询方法和
    使用SqlDependency类来获取
    通知更改。优点:$ b​​ $ b(可能?)比轮询
    方法少的流量。缺点:不是数据库
    独立的。 (我知道
    OracleDependency在ODP.NET,但什么
    关于其他数据库?)

什么方法更有利?也许我错过了在上述方法的一些主要(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。这不是你可能认为的交通量。如果你的数据不经常变化,你可以是悲观的,只是抓取一些东西,让你对表变化yay或不。

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'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(有效地),并返回更改的表的列表。

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#代码在这些更改上做一些工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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