How to fire NOTIFICATION event in front end when table data gets changed

后端 未结 1 541
余生分开走
余生分开走 2021-01-16 12:02

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

1条回答
  •  北荒
    北荒 (楼主)
    2021-01-16 12:15

    FRONT END:

    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;
    

    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.

    0 讨论(0)
提交回复
热议问题