更改表数据时如何在前端触发NOTIFICATION事件 [英] How to fire NOTIFICATION event in front end when table data gets changed
问题描述
我正在尝试利用vb.net中 Npgsql
给定的通知
事件。我对这种机制有部分了解,我了解到的是,只要更改了特定表的数据,就会触发其 trigger
,因此在其中的触发
,我们可以通知
到前端了解数据更改
。
I am trying to make use of the Notification
event given by Npgsql
in vb.net. I partially got an idea about this mechanism, what i learned was, when ever a particular table's data has got changed, its trigger
will be fired, so inside that trigger
we can notify
to the front end about the data change
.
我设法在前端运行以下代码
I managed to run this following code in my front end
Public Sub test()
Dim conn = New NpgsqlConnection("Server=servername;port=portNo; _
User Id=UID;pwd=PWD;DataBase=DB;")
conn.Open()
Dim command = New NpgsqlCommand("listen notifytest;", conn)
command.ExecuteNonQuery()
AddHandler conn.Notification, AddressOf NotificationSupportHelper
command = New NpgsqlCommand("notify notifytest;", conn)
command.ExecuteNonQuery()
End Sub
Private Sub NotificationSupportHelper(ByVal sender As Object, _
ByVal e As NpgsqlNotificationEventArgs)
'Notified here.
End Sub
上面的代码可以解决所有问题。但是我想知道的是如何创建触发器
,它将通知
有关前端数据更改的信息结果是我前端的 Notification事件
被解雇了?以及我需要在何处致电收听
??我需要为每个查询的执行调用监听
吗?有人可以用一些示例代码来澄清我的疑问吗??
The above code is working with out any issues. But what i am trying to know is how to create a trigger
which will notifies
about the data change to the front end as a result the Notification event
in my front end gets fired ? and where do i need to call listen
.? Do i need to call listen
for each query's execution .? Can any body will clarify my doubts with some sample codes.?
推荐答案
前端:
Public Sub test()
Dim conn = New NpgsqlConnection(" Server=server; port=portno;User Id=uid; pwd=pwd; DataBase=Db; ")
conn.Open()
Dim command = New NpgsqlCommand("listen notifytest;", conn)
command.ExecuteNonQuery()
AddHandler conn.Notification, AddressOf NotificationSupportHelper
command = New NpgsqlCommand("update testtable set field='test' where id=1", conn)
Dim x As NpgsqlDataReader = command.ExecuteReader
End Sub
Private Sub NotificationSupportHelper(ByVal sender As Object, ByVal e As NpgsqlNotificationEventArgs)
MsgBox(e.Condition)
End Sub
TRIGGER:
CREATE OR REPLACE FUNCTION testingNotify()
RETURNS trigger AS
$BODY$
begin
notify notifytest;
return null;
end;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
ALTER FUNCTION testingNotify() OWNER TO testserver;
以上给出的触发器
将被解雇插入
或删除
或更新
在表<$中c $ c> testtable 。所以在上面的代码中,在名为 test
的过程中,我编写了一个查询更新表tabletest
的查询,在执行查询时,会调用 NotificationSupportHelper
。
The trigger
given above will get fired if Insert
or delete
or update
happens in the table testtable
. so in the above code, inside the procedure named test
, i had written a query for updating the table tabletest
, so while executing the query the NotificationSupportHelper
gets called.
这篇关于更改表数据时如何在前端触发NOTIFICATION事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!