【问题标题】:Notify application for inserted or deleted rows通知应用程序插入或删除的行
【发布时间】:2016-03-27 06:43:32
【问题描述】:

我有一个 Microsoft SQL Server 2008 (SP3) 正在运行,并且想在其他一些无法访问的应用程序添加或删除某个表中的行时发出通知。我知道我可以使用触发器来操作插入或删除的数据(sql)等。但不知道如何监视表和通知。我要构建以显示通知的客户端可以是 wpf xaml 应用程序、通用 Windows 项目或 Web 应用程序。所以问题是监控表和触发发送某种通知(或休息网络呼叫或其他东西)之间的联系

我需要Notification Services 还是有更好/更简单的方法? 或者你会建议CLR trigger?还是WPF Data trigger?这对我来说似乎是最简单的。

编辑 我想要的输出将是一种显示如下内容的监控应用程序:

时间、产品代码、数量、删除/添加的内容如下:

  • 上午 9:45:添加了 2 台夏普电视
  • 上午 10:15:5 台飞利浦电视被移除

edit 2SqlDependency 提出了很多建议,但msdn 说:

一般来说,大多数非 ASP.NET 应用程序应该使用 SqlDependency 对象。 ASP.NET 应用程序应该使用更高级别的 SqlCacheDependency,它包装了 SqlDependency 并提供了一个用于管理通知和缓存对象的框架。

但我看不到如何查看已删除的行。因此,也许可以对表进行触发器以在消息表中插入消息 product 123 removed 5 times。和product 456 added 15 times在消息表中,然后对新创建的消息表有一个sqldependency?

【问题讨论】:

  • 我不确定,但你可以使用 sqldependency msdn.microsoft.com/en-us/library/62xk7953%28v=vs.110%29.aspx
  • 可以将 SqlDependency 对象与 SqlCommand 关联,以便检测查询结果何时与最初检索的结果不同。您还可以为 OnChange 事件分配一个委托,该事件将在关联命令的结果更改时触发。在执行命令之前,您必须将 SqlDependency 与命令相关联。 SqlDependency 的 HasChanges 属性还可用于确定自首次检索数据以来查询结果是否已更改。
  • 所以只是一个select * from table,然后是一个sqldependency来注意它是否有更多或更少的行。然后手动搜索它在哪几行?
  • 我想这个试试这个链接聊天经验:codeproject.com/Articles/12335/…

标签: c# sql-server triggers


【解决方案1】:

这里的审计是我的桌子,当桌子上发生一些事情时它会提出一些信息。

create trigger tr_audit_all on audit after delete,insert ,update
as begin
if exists (select 1 from inserted) and not exists (select 1 from deleted)
begin
print 'record inserted '
select * from inserted

end
if exists (select 1 from inserted) and exists (select 1 from deleted)
begin
raiserror( 'record inserted ',1,15)
end
if not exists (select 1 from inserted)and exists (select 1 from deleted )
begin
print 'record deleted '
end
end
drop trigger tr_audit_all

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-01-19
    • 2012-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多