Tracks changes on the server.
This sample demonstrates using
PgSqlDependency object to watch for modifications in the table test.dept and operations performed. It requires on PostgreSQL 9.0 or highter. It is assumed that you have the following objects created on the server:
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