如何监控应用程序的数据库更新?

发布于 2025-01-02 02:57:56 字数 494 浏览 2 评论 0原文

我使用 ODBC、C++ 来处理 SQL Server 数据库。我想检测数据库某些表中的修改:另一个应用程序插入或更新行,我必须检测所有这些修改。它不必是立即触发器,使用轮询定期检查数据库表是否有修改是可以接受的。

以下是我认为可以做到这一点的方法,并且需要您的意见这是否是执行此操作的标准/正确方法,或者是否存在更好的方法。

我的想法是这样的:我在 SQL Server 中添加触发器,在进行任何修改时,触发器会将修改/添加的行的标识符插入到特殊表中,我将定期从应用程序中检查该表。假设有 3 个表:客户、产品、服务。我将创建三个附加表:Change_Customers、Change_Products、Change_Services,并将插入相应表的已修改行的标识符。然后我将定期从应用程序中读取这些 Change_* 表并删除已处理的记录。

现在,如果您同意上述解决方案是正确的,我还有另一个问题:对于我希望监视的每个表都有单独的 Change_* 表更好,还是有一个包含以下更改的胖 Changes 表更好?所有桌子。

I work with SQL Server database with ODBC, C++. I want to detect modifications in some tables of the database: another application inserts or updates rows and I have to detect all these modifications. It does not have to be the immediate trigger, it is acceptable to use polling to periodically check database tables for modifications.

Below is the way I think this can be done, and need your opinions whether this is the standard/right way of doing this, or any better approaches exist.

What I've thought of is this: I add triggers in SQL Server, which, on any modification, will insert the identifiers of modified/added rows into special table, which I will check periodically from my application. Suppose there are 3 tables: Customers, Products, Services. i will make three additional tables: Change_Customers, Change_Products, Change_Services, and will insert the identifiers of modified rows of the respective tables. Then I will read these Change_* tables from my application periodically and delete processed records.

Now if you agree that above solution is right, I have another question: Is it better to have separate Change_* tables for each of my tables I wish to monitor, or is it better to have one fat Changes table which will contain the changes from all tables.

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

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

发布评论

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

评论(3

染柒℉ 2025-01-09 02:57:57

查询通知是一项旨在准确执行任务的技术 > 你所描述的。您可以通过众所周知的 SqlDependency 从托管客户端利用查询通知 类,但也有本机 Ole DB 和 ODBC 方式。请参阅使用查询通知,有关SSPROP_QP_NOTIFICATION_MSGTEXT (OleDB) 和 <代码>SQL_SOPT_SS_QUERYNOTIFICATION_MSGTEXT (ODBC)。有关查询通知如何工作的说明,请参阅神秘通知

这是唯一适用于任何类型更新的免轮询解决方案。触发器和更改轮询存在严重的可扩展性和性能问题。更改数据捕获和更改跟踪实际上涵盖了不同的主题(同步偶尔连接的设备的数据集,例如同步框架)。

Query Notifications is the technology designed to do exactly what you're describing. You can leverage Query Notifications from managed clients via the well known SqlDependency class, but there are native Ole DB and ODBC ways too. See Working with Query Notifications, the paragraphs about SSPROP_QP_NOTIFICATION_MSGTEXT (OleDB) and SQL_SOPT_SS_QUERYNOTIFICATION_MSGTEXT (ODBC). See The Mysterious Notification for an explanation how Query Notifications work.

This is the only polling-free solution that work with any kind of updates. Triggers and polling for changes has severe scalability and performance issues. Change Data Capture and Change Tracking are really covering a different topic (synchronizing datasets for occasionally connected devices, eg. Sync Framework).

分開簡單 2025-01-09 02:57:57

更改数据捕获(CDC)--http://msdn.microsoft.com/en-us/library/cc645937.aspx

首先,您需要在数据库中启用 CDC
::
使用数据库名称

执行sys.sp_cdc_enable_db
GO

然后在表上启用 CDC
:: sys.sp_cdc_enable_table

然后就可以查询变化

Change Data Capture(CDC)--http://msdn.microsoft.com/en-us/library/cc645937.aspx

First you will need to enable CDC in database
::
USE db_name
GO
EXEC sys.sp_cdc_enable_db
GO

Enable CDC on table then
:: sys.sp_cdc_enable_table

Then you can query changes

江湖彼岸 2025-01-09 02:57:57
  • 如果您的 Sql Server 版本是 2005 - 您可以使用 通知服务
  • 如果您的 Sql Server 是 2008+ - 有最好的方法使用触发器并将更改记录到日志表中,并定期从应用程序轮询这些表以查看变化
  • If your version of Sql Server is 2005 - you may use Notification Services
  • If your Sql Server is 2008+ - there is most preferrable way to use triggers and log changes to log tables and periodically poll these tables from application to see the changes
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文