'Declaration Public Class PgSqlDependency Implements System.IDisposable
public class PgSqlDependency : System.IDisposable
'Declaration Public Class PgSqlDependency Implements System.IDisposable
public class PgSqlDependency : System.IDisposable
The PgSqlDependency class is designed to watch for changes in tables on the server. This is accomplished using LISTEN ... NOTIFY server side mechanism and recurring check requests.
To start watching for a table in a schema perform the following steps:
Once you perform these steps the the PgSqlDependency object will poll the server periodically, and if the table is modified since the last call, it will invoke your event handler.
Note that you can use single PgSqlDependency instance to watch for several tables. To do this, specify all necessary tables in the FROM clause of command's SQL statement.
Note also that PgSqlDependency can watch for whole tables only. If you restrict the SELECT statement with WHERE clause, it will not affect notifications.
In PostgreSQL 9.0 and higher, you can also get the infiormation about the table change, that is passed as a second parameter of the NOTIFY command.
This class is not available in Mobile Edition.
CREATE OR REPLACE FUNCTION test.note_dept() RETURNS TRIGGER AS BEGIN PERFORM pg_notify('table_changed_test.dept', TG_OP); RETURN NULL; END; LANGUAGE plpgsql; CREATE TRIGGER notify_dept AFTER INSERT OR UPDATE OR DELETE ON test.dept EXECUTE PROCEDURE test.note_dept();
PgSqlConnection connection = new PgSqlConnection("User Id = postgres; Password = postgres; host = localhost; Port = 5432; database = testdb;"); PgSqlDependency depend = new PgSqlDependency(); depend.OnChange += new OnChangeEventHandler(dependency_OnChange); PgSqlCommand selectCommand = new PgSqlCommand("SELECT * FROM test.dept", connection); depend.AddCommandDependency(selectCommand); depend.CheckTimeout = 500; connection.Open(); try { depend.Start(connection); //different actions with data and check correct depend work PgSqlCommand commandInsert = new PgSqlCommand("INSERT INTO test.dept VALUES (111, 'test', 'test');", connection); PgSqlCommand commandUpdate = new PgSqlCommand("UPDATE test.dept SET dname = 'test' WHERE deptno = 10;", connection); PgSqlCommand commandDelete = new PgSqlCommand("DELETE FROM test.dept WHERE dname = 'test';", connection); commandInsert.ExecuteNonQuery(); System.Threading.Thread.Sleep(600); commandUpdate.ExecuteNonQuery(); System.Threading.Thread.Sleep(600); commandDelete.ExecuteNonQuery(); System.Threading.Thread.Sleep(600); } finally { depend.Stop(); connection.Close(); } ... private void dependency_OnChange(object sender, PgSqlTableChangeEventArgs e) { MessageBox.Show("Table changed: " + e.TableName + "; Operation: " + e.Info); }
Dim connection As New PgSqlConnection("User Id = postgres; Password = postgres; host = localhost; Port = 5432; database = postgres;") Dim depend As New PgSqlDependency AddHandler depend.OnChange, New OnChangeEventHandler(AddressOf Me.dependency_OnChange) Dim selectCommand As New PgSqlCommand("SELECT * FROM test.dept", connection) depend.AddCommandDependency(selectCommand) depend.CheckTimeout = 500 connection.Open() Try depend.Start(connection) Dim commandInsert As New PgSqlCommand("INSERT INTO test.dept VALUES (111, 'test', 'test');", connection) Dim commandUpdate As New PgSqlCommand("UPDATE test.dept SET dname = 'test' WHERE deptno = 10;", connection) Dim commandDelete As New PgSqlCommand("DELETE FROM test.dept WHERE dname = 'test';", connection) commandInsert.ExecuteNonQuery() System.Threading.Thread.Sleep(600) commandUpdate.ExecuteNonQuery() System.Threading.Thread.Sleep(600) commandDelete.ExecuteNonQuery() System.Threading.Thread.Sleep(600) Finally depend.Stop() connection.Close() End Try ... Private Sub dependency_OnChange(ByVal sender As Object, ByVal e As PgSqlTableChangeEventArgs) MessageBox.Show("Table changed " & e.TableName & "; Operation: " & e.Info) End Sub
System.Object
Devart.Data.PostgreSql.PgSqlDependency
Target Platforms: Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2