postgresql notify not return anything when insert

2019-09-07 17:10发布

问题:

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