我是postgresql的新手,现在我尝试从db获取通知,当那个表(我的表名是foo2)有插入或更新但没有从db返回到我的代码时使用c#project
这是我的代码
// -------------- 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";
}
-------------结束代码后端-------------------
希望你能理解我,因为我英语不强,谢谢