1

我正在尝试利用vb.netNotification中给出的事件。Npgsql我对这个机制有部分了解,我学到的是,当一个特定表的数据发生变化时,它trigger会被触发,所以trigger我们可以notify在前端了解data change.

我设法在我的前端运行以下代码

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

上面的代码没有任何问题。但是我想知道的是如何创建一个trigger关于notifies数据更改到前端的内容,结果Notification event我的前端被解雇了?我需要在哪里打电话listen?我需要调用listen每个查询的执行吗?任何机构都可以用一些示例代码来澄清我的疑问吗?

4

1 回答 1

0

前端:

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

扳机:

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;

如果或发生在 table 中,上面trigger给出的将被解雇。所以在上面的代码中,在名为 的过程中,我写了一个查询,所以在执行查询时被调用。Insertdeleteupdatetesttabletestupdating the table tabletestNotificationSupportHelper

于 2013-03-18T09:37:46.903 回答