数据库触发器还是代码中的常用方法?

发布于 2024-09-14 20:11:13 字数 445 浏览 2 评论 0原文

我有一个表,我想在其中记录应用程序某些部分的活动。当在其他表中插入/更新记录时,将在该表中插入一条记录(将来可能会更新)。

例如,

  • 如果记录插入到 Orders 表中 将在日志中插入一个条目 桌子。
  • 如果在 Booking 中插入记录 表中将插入一个条目 日志表。
  • 如果客户中的记录已更新 表中将插入一个条目 日志表如果日志表没有 该客户的条目。

等等..

我应该在这些表上使用触发器在日志表中添加记录,还是应该在代码中使用通用方法并在发生插入/更新活动时调用该方法?

我必须在应用程序的某些部分执行此活动,因此可能有超过 20 个表,我将在其中添加触发器,或者从几个不同的位置调用方法。

我使用的是 SQL Server 2005 和 C#

触发器和方法哪个更好?

I have a table where I want to Log activities of some part of my application. A record will be inserted (may be updated in future) in this table when record inserted/updated in some other table.

E.g.

  • If record is inserted in Orders table
    an entry will be inserted in Log
    table.
  • If record is inserted in Booking
    table an entry will be inserted in
    Log table.
  • if record is updated in Customers
    table an entry will be inserted in
    Log table if Log table does not have
    an entry for this customer.

etc..

Should I use Triggers on these tables to add records in Log table or should I have a common method in my code and call that method whenever insert/update activity occurs?

I have to do this activity on some part of my applications so there can be more than 20 tables on which I will be adding trigger or from couple of different locations from where I will call method.

I am using SQL Server 2005 and C#

What is better, Trigger or A method?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

遇到 2024-09-21 20:11:13

方法是比触发器更好的选择。
触发因素一般是
- 性能重
- 在代码中不太明显,即隐藏起来
- 更难调试和维持。
- 对传递到日志表的值的限制

方法将在优化代码、扩展逻辑和更易于维护方面给您带来很多优势

Method is better option than Trigger.
Triggers are generally
- performance heavy
- Less visible in the code, ie are hidden away
- More difficult to debug & Maintain.
- Limits on values to be passed to the log table

A method would give you lots of advantages in terms of optimizing the code, and extending the logic and easier to maintain

深者入戏 2024-09-21 20:11:13

由于这似乎是一项重要任务,我将在 RDBMS 内使用触发器来确保不仅您的应用程序会导致创建日志。

As this seems an important task I would use triggers inside the RDBMS to ensure that not only your application causes the logs to be created.

苏辞 2024-09-21 20:11:13

如果有人能够使用 TOAD、SSMS、查询分析器等技术在没有您的应用程序的情况下更新数据库,那么触发器会更好

In case someone has the ability to update the database without your app by using TOAD, SSMS, Query Ananlyzer etc tec, a trigger would be better

生寂 2024-09-21 20:11:13

提出此类问题永远不会太晚,
一般来说,触发器减少了数据库和代码的往返,
在您的情况下,要在 C# 中执行此操作,每个操作需要 2 次,一次用于操作(插入),一次用于日志操作,当然您需要对代码中的异常进行大量处理,因此如果记录未插入,您可以处理此问题,并且还可以记录不同的失败操作
作为触发器,您将数据发送到服务器一次,所有操作和处理都在那里完成,无需额外连接
这在现在所有东西都是共享的并且连接轮询受到限制的情况下特别有用。

it is never too late for such questions ,
in General , triggers reduce the round trip of your DB and code ,
in your case , to do this in C# you will need 2 trips for each action ,one for the action (Insert) and one for the log action , and of course you need to do a lot of handling for exceptions in your code so if the record is not inserted you handle this and also you log different action of failure
as trigger ,you send the data once to the server and all actions and handling are done there with no extra connections
this is useful specially now that every thing is shared and connections polls are limited .

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文