用戶需要授予權限 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;
