在表更新,在我的.NET代码触发一个动作 [英] On table update, trigger an action in my .NET code

查看:145
本文介绍了在表更新,在我的.NET代码触发一个动作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道这是否是可能的。我们希望有一个功能,在我们的.NET代码工作,当在特定的表中的值被更新。这可能是在一个记录插入或更新。这可能吗?
。如果没有,有没有一种替代方法?

I'm wondering whether this is possible. We want a function to work in our .NET code when a value in a specific table is updated. This could be upon a record insert or update. Is this possible? If not, is there an alternative process?

推荐答案

您需要问几个问题。

你想要的任何以不关你的业务逻辑的分贝水平?
显然,一个数据库触发器能做到这一点(当值改变执行某些操作,即使非常具体的值仅)。

Do you want any to none of your business logic at the db level? Obviously a db trigger could do this (perform some action when a value is changed, even if very specific value only).

我见过一些系统被触发分贝重。他们的逻辑驻留深和高度再加分贝平台。有一些优势的,但大多数人可能会说,缺点是太大(耦合,缺乏encapuslation /可重用性)。

I've seen some systems that are db trigger heavy. Their 'logic' resides deeply and highly coupled with the db platform. There are some advantages to that, but most people would probably say the disadvantages are too great (coupling, lack of encapuslation/reusability).

根据你在做什么和你的倾向,你可以:

Depending on what you are doing and your leanings you could:


  1. 确认所有的DAO / BusinessFunctoin对象调用你的事件 object.function 做的时候一定值发生改变时,你想要什么。

  1. Make sure all DAO/BusinessFunctoin objects call your 'event' object.function to do what you want when a certain value change occurs.

使用触发器打电话给你的事件时一定值发生变化 object.function

Use a trigger to call your 'event' object.function when a certain value change occurs.

您触发所做的一切。

我个人偏向选择2,你有一个最小的触发器(它只是触发事件调用你的 object.function ),所以你不深的情侣你的数据库到你业务逻辑。

I personally would lean towards Option 2 where you have a minimal trigger (which simply fires the event call to your object.function) so you don't deeply couple your db to your business logic.

选项1是好的,但可能会有点麻烦,除非你有一个非常狭隘的高炉/ DAO的那些话你想要这个分贝table.field到观看。

Option 1 is fine, but may be a bit of a hassle unless you have a very narrow set of BF/DAO's that talk to this db table.field you want to watch.

选项3恕我直言是最糟糕的选择,因为你的情侣逻辑您的数据库,并降低其可访问到你的商业逻辑层。

Option 3 is imho the worst choice as you couple logic to your db and reduce its accessibility to your business logic layer.

鉴于此,这里是朝着通过选项2完成一些这方面的信息:

使用从MSDN这个例子: http://msdn.microsoft.com/en-美国/库/ 938d9dz2.aspx

Using this example from MSDN: http://msdn.microsoft.com/en-us/library/938d9dz2.aspx.

这显示了如何有一个触发并调用项目中的CLR对象。

This shows how to have a trigger run and call a CLR object in a project.

实际上,在你的项目,你创建触发器,并调用它的类。

Effectively, in your project, you create a trigger and have it call your class.

注意行: [SqlTrigger(NAME =UserNameAudit,目标=用户,事件=表示插入)]

这定义在代码大火,然后内的代码,你可以检查你的约束,则火法的其余部分(或没有),或拨打另一个的object.method 需要。

This defines when the code fires, then within the code, you can check your constraint, then fire the rest of the method (or not), or call another object.method as needed.

直接进入数据库,并添加触发器之间的主要区别是给人当一起部署您访问项目中的所有对象。

The primary difference between going directly to the db and adding a trigger is this gives you access to all the objects in your project when deployed together.

这篇关于在表更新,在我的.NET代码触发一个动作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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