postgresql notify not return anything when insert or update

佐手、 提交于 2019-12-12 01:29:46

问题


i'm newbie for postgresql, now i try to get notification from db when that table (my table name is foo2) have insert or update but not have anything return from db to my code with c# project

This is my code

 // -------------- create table ---------------
 CREATE TABLE foo2 (id serial primary key, name varchar);

 // -------------- create function ------------
 CREATE OR REPLACE FUNCTION nf() RETURNS TRIGGER AS $$
 BEGIN
    PERFORM pg_notify('notifytest', format('INSERT %s %s', NEW.id,    NEW.name));
 RETURN NULL;
 END;
 $$ LANGUAGE plpgsql;

 // ------------ create trigger -----
 CREATE TRIGGER any_after AFTER INSERT OR UPDATE OR DELETE ON foo2 FOR EACH ROW EXECUTE PROCEDURE nf();

// ------------- my backend code from asp.net c#
protected void Page_Load(object sender, EventArgs e)
    {
        test();

    }

public void test(){
        NpgsqlConnection conn = new NpgsqlConnection("Server=server name;port=post;User Id=user;pwd=pass;DataBase=dbName;");
        conn.Notification += NotificationSupportHelper;
            conn.Open();
            using (var command = new NpgsqlCommand("listen notifytest;", conn))
            {
                command.ExecuteNonQuery();
            }
            System.Diagnostics.Debug.WriteLine("This will be displayed in output window");
            Console.ReadLine();

    }

    private void NotificationSupportHelper(object sender, NpgsqlNotificationEventArgs e)
    {
        string test = "income";

    }

------------- end code backend -------------------

Hope you can understand me, cuz i'm not strong in english, Thanks

来源:https://stackoverflow.com/questions/41727428/postgresql-notify-not-return-anything-when-insert-or-update

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!