C# 在表更新时,在我的 .NET 代码中触发一个操作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18318178/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
On table update, trigger an action in my .NET code
提问by alwaysVBNET
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?
我想知道这是否可能。当特定表中的值更新时,我们希望函数在我们的 .NET 代码中工作。这可能是在记录插入或更新时。这可能吗?如果没有,是否有替代流程?
采纳答案by williambq
You need to ask a couple of questions.
你需要问几个问题。
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).
您是否希望在 db 级别没有任何业务逻辑?显然 db 触发器可以做到这一点(当值改变时执行一些操作,即使只是非常具体的值)。
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).
我见过一些数据库触发很重的系统。他们的“逻辑”与 db 平台高度耦合。这有一些优点,但大多数人可能会说缺点太大了(耦合、缺乏封装/可重用性)。
Depending on what you are doing and your leanings you could:
根据您在做什么和您的倾向,您可以:
Make sure all DAO/BusinessFunctoin objects call your 'event'
object.function
to do what you want when a certain value change occurs.Use a trigger to call your 'event'
object.function
when a certain value change occurs.Your trigger does everything.
确保所有 DAO/BusinessFunctoin 对象都调用您的“事件”
object.function
以在发生特定值更改时执行您想要的操作。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.
我个人会倾向于选项 2,其中您有一个最小的触发器(它只是将事件调用触发到您的object.function
),因此您不会将您的数据库与您的业务逻辑深度耦合。
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.
选项 1 很好,但可能有点麻烦,除非您有一组非常窄的 BF/DAO 与您想要观看的这个 db table.field 对话。
Option 3 is imho the worst choice as you couple logic to your db and reduce its accessibility to your business logic layer.
恕我直言,选项 3 是最糟糕的选择,因为您将逻辑耦合到数据库并降低其对业务逻辑层的可访问性。
Given that, here is some information toward accomplishing this via Options 2:
鉴于此,以下是通过选项 2 实现此目的的一些信息:
Using this example from MSDN: http://msdn.microsoft.com/en-us/library/938d9dz2.aspx.
使用 MSDN 中的此示例:http: //msdn.microsoft.com/en-us/library/938d9dz2.aspx。
This shows how to have a trigger run and call a CLR object in a project.
这显示了如何在项目中运行触发器并调用 CLR 对象。
Effectively, in your project, you create a trigger and have it call your class.
实际上,在您的项目中,您创建了一个触发器并让它调用您的类。
Notice the line: [SqlTrigger(Name="UserNameAudit", Target="Users", Event="FOR INSERT")]
注意这一行: [SqlTrigger(Name="UserNameAudit", Target="Users", Event="FOR INSERT")]
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.
这定义了代码何时触发,然后在代码中,您可以检查约束,然后触发方法的其余部分(或不触发),或者object.method
根据需要调用另一个。
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.
直接进入数据库和添加触发器之间的主要区别在于,当部署在一起时,这使您可以访问项目中的所有对象。