简体   繁体   English

监视SQL数据库表更改的设计注意事项

[英]Design considerations for monitoring SQL database table changes

I am working on an assignment to monitor and log various "system-level messages” within our multi-tier .NET 4 WPF, WCF, SQL 2008 application. By messages, I mean details about an event that has occurred in our application, eg user logs in and out or data is saved. By logging, I mean that we will be inserting messages into a light-weight SQL table from which clients can poll and display the latest messages. 我正在分配一个任务,以监视和记录多层.NET 4 WPF,WCF,SQL 2008应用程序中的各种“系统级消息”,用消息表示我的应用程序中发生的事件的详细信息,例如用户登录和注销或保存数据通过登录,我的意思是我们将把消息插入一个轻量级的SQL表中,客户端可以从中查询并显示最新消息。

The source of these messages could come from several different components in our application eg Windows Services, WCF Host in IIS, and even stored procedures in the Database. 这些消息的来源可能来自我们应用程序中的几个不同组件,例如Windows服务,IIS中的WCF主机,甚至数据库中的存储过程。 Ultimately these components modify SQL tables each in their own way. 最终,这些组件以各自的方式修改SQL表。 So rather than attacking each of these components independently, I'm thinking I might be able to simplify things by saying that the messages need to be “triggered” when certain tables in SQL are modified (updated, inserted). 因此,我认为我可以通过说在修改(更新,插入)SQL中的某些表时需要“触发”消息来简化事情,而不是独立地攻击每个组件。

The first thing that comes to mind would be a trigger on each of the tables which monitor changes and insert records into the light-weight message table. 首先想到的是每个表上的触发器,该表监视更改并将记录插入轻量级消息表中。 I've always (99% of the time) been of the mindset that database triggers are bad news ( Are database triggers evil? ). 我一直(99%的时间)一直认为数据库触发器是个坏消息( 数据库触发器是邪恶的吗? )。 And I would personally much rather develop and debug C# than a SQL trigger. 我个人宁愿开发和调试C#,也不愿使用SQL触发器。

So, in the pursuit of alternatives, I've come across Using SqlDependency to Monitor Database Changes which discusses using the SqlDependency class to detect changes. 因此,在寻求替代方案时,我遇到了使用SqlDependency监视数据库更改的问题 ,该部分讨论了如何使用SqlDependency类检测更改。 A quick proof of concept seems to work; 快速概念验证似乎有效; however, after looking at several code samples, it looks like with each change that's detected, new SqlConnection, SqlCommand, and SqlDependency objects will have to be reinitialized and I will probably need 3 or 4 queries that will need to be monitored. 但是,在查看了几个代码示例后,似乎检测到每个更改,都必须重新初始化新的SqlConnection,SqlCommand和SqlDependency对象,我可能需要监视3或4个查询。

Are there better options to detecting changes to SQL tables in C#? 有更好的选择来检测C#中的SQL表更改吗?

Also, trigger bias aside, this seems like round-about way to simply monitor changes in one set of tables just to re-insert data into another table. 此外,撇开触发器的偏见,这似乎是一种环回方法,可以简单地监视一组表中的更改,只是将数据重新插入到另一个表中。 There will be some other logic that will need to take place that I would rather implement in C#, but should I just write triggers to do this and be done with it? 我宁愿在C#中实现一些其他逻辑,但是我是否应该编写触发器来完成并完成呢?

Thoughts? 有什么想法吗?

I think your bias against triggers in general is unfounded. 我认为您对触发器的总体偏见是没有根据的。 People look down upon triggers for the same reason they look down on cursors: in many cases, they are misused and abused. 人们看不起触发器的原因与看不起光标的原因相同:在许多情况下,它们被滥用和滥用。 But like a lot of things they certainly have their place and, when used correctly, are the right answer. 但是,就像许多事物一样,它们当然也有其位置,如果正确使用, 它们是正确的答案。

There are some other native technologies you should look into: 您还应该研究其他一些本机技术:

Change Tracking 变更追踪
http://msdn.microsoft.com/en-us/library/cc280462(SQL.100).aspx http://msdn.microsoft.com/zh-CN/library/cc280462(SQL.100).aspx

Change Data Capture 更改数据捕获
http://msdn.microsoft.com/en-us/library/bb522489(SQL.100).aspx http://msdn.microsoft.com/zh-CN/library/bb522489(SQL.100).aspx

SQL Server Audit SQL Server审核
http://msdn.microsoft.com/en-us/library/cc280386(SQL.100).aspx http://msdn.microsoft.com/zh-CN/library/cc280386(SQL.100).aspx

Though I don't know your edition (some of these are Enterprise features). 虽然我不知道您的版本(其中一些是企业功能)。 Some 3rd party solutions exist as well (I haven't used any so I'll leave it up to you to search / research). 也存在一些第三方解决方案(我没有使用过任何解决方案,因此请您自行查找/研究)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM