c# - 通过 nPgSQL 接收 NOTIFY 负载

标签 c# .net database npgsql

目前,我正在使用 NPGSQL 连接器将 PostgreSQL 数据库连接到我用 VB.Net 编写的应用程序。

我尝试使用 PostgreSQL 的 LISTENNOTIFY 功能将特定表的表更改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/

相关文章:

c# - 由于缺少未丢失的程序集引用,Visual Studio 拒绝构建项目

database - 具有相同属性但不同关系的表是不好的做法吗?

c# - 在 Web Api 中使用 Postman 授权属性认证

c# - System.Object 类和结构之间的关系

c# - 当其他应用程序的用户登录问题 C# 时用户注销

c# - 不能从零开始的正则表达式范围

c# - 错误 : Native images generated against multiple versions of assembly System.Net.Http.Primitives

python - MYSQL 更新另一个表中的数据(如果存在),否则插入

android - 预填充数据库 - Android 应用程序

c# - 表示百分比、概率或库存水平