2013-03-18 50 views
1

我正嘗試使用vb.net中Npgsql給出的Notification事件。我部分了解了這種機制,我所學到的是,當某個表格的數據發生變化時,其trigger將被解僱,因此在trigger之內,我們可以將notify轉移到前端約data change如何在表格數據發生變化時觸發前端的NOTIFICATION事件

我設法在我的前端

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?任何機構都可以用一些示例代碼來澄清我的疑惑。

+1

@DOWNVOTER有膽量。?然後指定原因。 – 2013-04-10 08:55:09

回答

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 

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; 

上面給出如果Insertdeleteupdate發生在表testtable將被解僱的trigger。所以在上面的代碼中,在名爲test的過程中,我寫了一個查詢updating the table tabletest,因此在執行查詢時調用了NotificationSupportHelper

+0

您是否必須暫停您用來附加活動的連接? – 2016-04-19 05:18:41

相關問題