C# 监控Oracle数据变化

用户需要授予权限 grant change notification to hfspas;
public void GetDatabaseChange()
{
string sql = "select * from t_prescription_handwork where trunc(checkindate)=trunc(sysdate)";
string constr = ConfigurationManager.ConnectionStrings["oracle"].ConnectionString;
OracleConnection con = new OracleConnection(constr);
con.Open();
OracleCommand cmd = new OracleCommand(sql, con);
OracleDependency dep = new OracleDependency(cmd);
dep.QueryBasedNotification = false;
//是否在Notification中包含变化数据对应的RowId
dep.RowidInfo = OracleRowidInfo.Include;
dep.OnChange += new OnChangeEventHandler(OnDatabaseNotification);
//是否在一次Notification后立即移除此次注册
cmd.Notification.IsNotifiedOnce = false;
//此次注册的超时时间(秒),超过此时间,注册将被自动移除。0表示不超时。
cmd.Notification.Timeout = 0;
//False表示Notification将被存于内存中,True表示存于数据库中,选择True可以保证即便数据库重启之后,消息仍然不会丢失
cmd.Notification.IsPersistent = true;
cmd.ExecuteNonQuery();
cmd.Dispose();
con.Dispose();
}
 
public void OnDatabaseNotification(object src, OracleNotificationEventArgs args)
{
if (args.Info == OracleNotificationInfo.Insert)
{
DataTable changeDetails = args.Details;
//notificationReceived = true;
string rowid = changeDetails.Rows[0]["rowid"].ToString();
//查找这个对象
PrescriptionHandwork temp = PrescriptionHandwork.FetchPrescriptionHandworkByRowid(rowid);
 
switch (args.Info)
{
case OracleNotificationInfo.Insert:
UpdateAsyncGridControl(gridOrderlist, temp);
break;
}
}
}
 
 
grant change notification to hfspas;
 
原文地址:https://www.cnblogs.com/devgis/p/14180860.html