【问题标题】:Postgres C# Trigger FunctionPostgres C# 触发函数
【发布时间】:2018-06-30 18:55:54
【问题描述】:

我正在尝试在将新数据添加到数据库时调用一个函数。

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!");
    }

但是,当添加新数据时,我的消息框没有显示。在另一个使用相同触发函数的程序上具有 'SyncNotification=true;'在 conListenRun 结束时。

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

但是,当我输入“SyncNotification=true;”时在声明中我收到此错误:

: '不支持关键字:syncnotification 参数名称:关键字'

我做错了什么?

谢谢

【问题讨论】:

  • 看看这里:npgsql.org/doc/wait.html...Npgsql will only process it and emit an event to the user the next time a command is sent and processed。你能检查它是否适合你吗?
  • 不相关,但把它放在using 块中,那么你就不需要finally

标签: c# sql postgresql triggers


【解决方案1】:

我使用的是最新版本的 NPGSQL,3.6.2(我认为),而另一个项目使用的是 2.11.96 版本。由于使用旧版本,该程序可以正常工作。我猜较新的 NPGSQL 使用不同的方式来执行触发函数。

【讨论】:

    【解决方案2】:

    npgsql.dll 版本的示例代码
    知道这适用于 3.2.6.0 或 Postgres 10 及更高版本(这可能意味着 ngpsql.dll v3.0 及更高版本)。

    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");
    }
    

    【讨论】:

      【解决方案3】:

      您缺少connconnListenRun.WaitAsync();connconnListenRun.Wait();

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-04-17
        • 2021-12-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多