OracleCommand cmd =
new OracleCommand("select * from Test WHERE TestFLAG = 1 or TestFLAGis not null", con);
当桌子发生变化时,无论条件如何,我的 .net 项目仍然会收到通知。
对于第二个问题,在我第一次收到任何通知后,此后表上的任何更改都不会被通知。为什么?
我的问题有什么解决方案吗?
public class MyNotificationSample
{
static string constr = "your db INFO";
public static bool IsNotified = false;
static OracleDependency dep = null;
public static void Main(string[] args)
{
//To Run this sample, make sure that the change notification privilege
//is granted to scott.
OracleConnection con = null;
try
{
con = new OracleConnection(constr);
OracleCommand cmd = new OracleCommand("select * from Test WHERE TestFLAG = 1 or TestFLAGis not null", con);
con.Open();
// Set the port number for the listener to listen for the notification
// request
OracleDependency.Port = 1005;
// Create an OracleDependency instance and bind it to an OracleCommand
// instance.
// When an OracleDependency instance is bound to an OracleCommand
// instance, an OracleNotificationRequest is created and is set in the
// OracleCommand's Notification property. This indicates subsequent
// execution of command will register the notification.
// By default, the notification request is using the Database Change
// Notification.
dep = new OracleDependency(cmd);
// Add the event handler to handle the notification. The
// OnMyNotification method will be invoked when a notification message
// is received from the database
dep.OnChange += OnMyNotificaton;
// The notification registration is created and the query result sets
// associated with the command can be invalidated when there is a
// change. When the first notification registration occurs, the
// notification listener is started and the listener port number
// will be 1005.
cmd.ExecuteNonQuery();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
con.Close();
Console.Write("Press Any Key to Quit");
Console.ReadLine();
// Loop while waiting for notification
}
public static void OnMyNotificaton(object src,
OracleNotificationEventArgs arg)
{
if (dep.HasChanges)
{
Console.WriteLine("Notification Received");
DataTable changeDetails = arg.Details;
Console.WriteLine("Data has changed in {0}",
changeDetails.Rows[0]["ResourceName"]);
}
}
最新更新:使监听器永不过期。
new OracleDependency(cmd, false, 0 , true);
但是,我的查询仍然不起作用......