目前,我正在使用 NPGSQL
连接器将 PostgreSQL 数据库连接到我用 VB.Net
编写的应用程序。
我尝试使用 PostgreSQL 的 LISTEN
和 NOTIFY
功能将特定表的表更改NOTIFY
发送到前端。我从那次尝试中得到了积极的结果。成功的从后端通知到前端。为了实现这一目标,我尝试了以下方法:
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
这工作正常,但是,PostgreSQL 有一个全新的通知功能,其中通知可以有一个有效载荷,比如 NOTIFY test, 'Hello world'
(其中 Hello world 是有效载荷,在换言之,附加信息)。
我想在通知事件处理程序 NotificationSupportHelper
中获取附加信息。因此,为了得到它,我只是尝试访问 NpgsqlNotificationEventArgs
的成员。但是我在那次尝试中失败了,因为它只包含 PID 和 Condition 作为其成员。所以我决定看看 NPGQL documentation ,从那里,我得到了,没有办法在 NPGSQL vers 1.0 中获得额外的信息。但是他们为 NpgsqlNotificationEventArgs
提供了一个名为 AdditionalInformation 的额外成员在他们的第二个版本(NPGSQL V 2.0)中。但它目前也在 build 中。此属性将始终返回 String.Empty(来自 docs)。
所以我希望你们理解我正在努力完成的事情,我只需要在引发通知
时在前端接收payload
。有人知道如何实现我的要求吗?是否有任何其他可用的连接器
而不是NPGSQL
来有效地处理前端的通知事件
,这意味着获取有效负载
在前端。?
最佳答案
以防其他人有这个问题。 AdditionalInformation
属性现已可用。
Private Sub NotificationSupportHelper(ByVal sender As Object,
ByVal e As NpgsqlNotificationEventArgs)
MsgBox(e.Condition & " " & e.AdditionalInformation)
End Sub
关于c# - 通过 nPgSQL 接收 NOTIFY 负载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16098770/