简体   繁体   中英

Postgres C# Trigger Function

I am trying to call a function when new data is add to the database.

NpgsqlConnection connListenRun = new NpgsqlConnection("Server=main;Port=5432;Database=netdb;UserId=postgres;Password=password;");
        try
        {
            connListenRun.Open();
            NpgsqlCommand cmd = new NpgsqlCommand("listen RunLogClient;", connListenRun);
            cmd.ExecuteNonQuery();
            connListenRun.Notification += new NotificationEventHandler(RunFinishNotification);
        }
        catch (NpgsqlException ex)
        {
            MessageBox.Show(ex.ToString());
        }
        finally
        {
            //connListen.Close();
        }

private void RunFinishNotification(object sender, NpgsqlNotificationEventArgs e)
    {
        MessageBox.Show("Data!");
    }

However, my message box isn't showing when new data is added. On another program using the same trigger function has 'SyncNotification=true;' on the end of the conListenRun.

NpgsqlConnection connListenRun = new NpgsqlConnection("Server=main;Port=5432;Database=netdb;UserId=postgres;Password=password;SyncNotification=true;");

However, when i put 'SyncNotification=true;'on the statment i get this error:

: 'Keyword not supported: syncnotification Parameter name: keyword'

What am i doing wrong?

Thanks

I was using the latest version of NPGSQL, 3.6.2 (I think) where as the other project was using version 2.11.96. Since using the older version the program works. I guess the newer NPGSQL uses a different way to do trigger functions.

Sample code with the npgsql.dll version
Know this works for 3.2.6.0 or form Postgres 10 onwards (which would likely mean ngpsql.dll v3.0 onwards).

public static void Main(string[] args)   
{
    bool flag = true;
    string server = "127.0.0.1";
    string port = "5432";
    string database = "postgres";
    string uid = "postgres";
    string password = "postgres";
    string connStr;

    // the connection string
    connStr = $"SERVER = {server}; Port = {port};  DATABASE = {database}; User Id = 
    {uid}; PASSWORD = {password};"; 
    NpgsqlConnection notificationConnection;
    NpgsqlConnectionStringBuilder csb = new NpgsqlConnectionStringBuilder(connStr);


    try
    {
        notificationConnection = new NpgsqlConnection(connStr);
        notificationConnection.Open();
        if (notificationConnection.State != System.Data.ConnectionState.Open)
        {
            WriteLine("Connection to database failed");
            return;
        }

        using (NpgsqlCommand cmd = new NpgsqlCommand($"LISTEN {notificationName}", 
               notificationConnection))
        {
            cmd.ExecuteNonQuery();
        }

        notificationConnection.Notification += PostgresNotification;
        notificationConnection.WaitAsync();
    }   
    catch(Exception ex)
   {
       WriteLine($"Exception thrown with message : {ex.Message}");
       return;
   }

   //  wait forever, press enter key to exit program  
   ReadLine();

   // stop the db notifcation
   notificationConnection.Notification -= PostgresNotification;
   using (var command = new NpgsqlCommand($"unlisten {notificationName}", 
      notificationConnection))
   {
       command.ExecuteNonQuery();
   }
       notificationConnection.Close();
}






//  the callback method that handles notification
static void PostgresNotification(object sender, NpgsqlNotificationEventArgs e)
{
     WriteLine("Notification Received");
}

you are missing connconnListenRun.WaitAsync(); or connconnListenRun.Wait();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM