我使用OracleDependency時遇到了一些問題。OracleDependency事件未觸發
我已閱讀MSDN和Oracle文檔,並複製一些代碼來嘗試它。
但是,這不起作用,插入完成時事件on_my_event不會觸發。
有誰知道爲什麼?
我的用戶對數據庫有CHANGE NOTIFICATION權限。 Oracle Server是11.2.0.3.0。
下面是代碼:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Oracle.DataAccess.Client;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
string connection_string = "Data Source=My_srv;User Id=My_usr;Password=My_pwd;";
OracleConnection connection = null;
OracleDependency dependency = null;
OracleCommand my_select = null;
OracleCommand my_insert = null;
public Form1()
{
InitializeComponent();
}
private void TB_insert_event(object sender, EventArgs e)
{
if (TB_insert.Text == "Name of your insert")
TB_insert.Clear();
}
private void insert(object sender, EventArgs e)
{
connection = new OracleConnection(connection_string);
my_insert = connection.CreateCommand();
my_insert.CommandText = "INSERT INTO USR_DEV_TRUNK.WPARAM (wpa_codeparam) VALUES ('" + TB_insert.Text + "')";
connection.Open();
my_insert.ExecuteNonQuery();
connection.Close();
}
private void Set_dep(object sender, EventArgs e)
{
OracleDependency.Port = 3048;
connection = new OracleConnection(connection_string);
connection.Open();
my_select = connection.CreateCommand();
my_select.CommandText = "SELECT wpa_codeparam FROM USR_DEV_TRUNK.WPARAM";
dependency = new OracleDependency();
dependency.AddCommandDependency(my_select);
my_select.Notification.IsNotifiedOnce = false;
my_select.ExecuteNonQuery();
dependency.OnChange += new OnChangeEventHandler(on_my_event);
TB_dependency.Text = "The dependency is set, do your insert to see if it works";
connection.Close();
}
public void on_my_event(object obj, OracleNotificationEventArgs arg)
{
TB_dependency.Text = "Yay ! It worked !";
}
}
}
我有兩個按鈕:
之一設置我的依賴關係(函數(上點擊):Set_dep)
一個做我的插入(功能(點擊):插入)
而我有兩個文本框:
一個讓我插入(名稱:TB_insert)
一個顯示依賴狀態(名稱:TB_dependency)
前一段時間,@LadislavM發現[服務器和客戶端版本完全匹配]很重要(http://stackoverflow.com/questions/10932968/change-notification-with-oracle-dependency-in-c-sharp) 。你檢查了嗎? 11.2.0.3.0應該沒問題。 – mkoertgen 2013-08-04 14:55:39