I am developing a asp.net web api application where I'm trying to get oracle database change notification in c#. But when I am Initializing 1 oracle dependency, everything is working as expected but when I am trying to Initialize 2 oracle dependency from 2 different oracle database, I am getting an error "'This operation requires the objects to use the same connection'"
var cmd = new OracleCommand("SELECT EMPNO FROM EMP r WHERE rownum <= 1 Order by EMPNO Desc", con1);
con1.Open();
cmd.AddRowid = true;
var dep = new OracleDependency(cmd);
dep.OnChange += new OnChangeEventHandler(dep_OnChange);
DataTable dt = new DataTable();
dt.Load(cmd.ExecuteReader());
var cmd2 = new OracleCommand("SELECT EMPNO FROM EMP2 r WHERE rownum <= 1 Order by EMPNO Desc", con2);
con2.Open();
cmd2.AddRowid = true;
var dep2 = new OracleDependency(cmd2);
dep2.OnChange += new OnChangeEventHandler(dep2_OnChange);
DataTable dt2 = new DataTable();
dt2.Load(cmd2.ExecuteReader());
firstly this step by step guide may resolve your issue: Oracle Dependency Guide
Secondly, this is also another work around you can use to do your job.
Yes there can be. Your Database layer should contain 2 classes which will connect to the respective database. Plus don't use ExecuteReader
, instead use DataAdapter
because it handles Connection.Open(), Close(), Dispose() itself.
for example
DATABASE1.CS
public class Database1
{
string sCon = string.Empty;
OracleConnection OraCon;
protected string query = string.Empty;
public Database1()
{
sCon = "Data Source=(DESCRIPTION =(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST = 1.1.1.1)(PORT = 12345)))(CONNECT_DATA =(SID = SIDNAME)));User ID=username;Password=pass;";
OraCon = new OracleConnection(sCon);
}
protected DataTable FillDataTableByParam(OracleParameter[] param)
{
DataTable oDT = new DataTable();
OracleCommand OraCom = new OracleCommand(query, OraCon);
OraCom.Parameters.AddRange(param);
new OracleDataAdapter(OraCom).Fill(oDT);
query = "";
return oDT;
}
}
DATABASE2.CS
public class Database2
{
string sCon = string.Empty;
OracleConnection OraCon;
protected string query = string.Empty;
public Database2()
{
sCon = "Data Source=(DESCRIPTION =(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST = 1.1.1.1)(PORT = 12345)))(CONNECT_DATA =(SID = SIDNAME2)));User ID=username;Password=pass;";
OraCon = new OracleConnection(sCon);
}
protected DataTable FillDataTableByParam(OracleParameter[] param)
{
DataTable oDT = new DataTable();
OracleCommand OraCom = new OracleCommand(query, OraCon);
OraCom.Parameters.AddRange(param);
new OracleDataAdapter(OraCom).Fill(oDT);
query = "";
return oDT;
}
}
But you can also keep 1 class in your Database Layer making it accessible for 2 databases, it would look something like this:
public class DatabaseLayer
{
string sCon = string.Empty;
OracleConnection OraCon;
protected string query = string.Empty;
public DatabaseLayer(string DataBaseSecureName)
{
if(DataBaseSecureName == "One")
{
sCon = "Data Source=(DESCRIPTION =(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST = 1.1.1.1)(PORT = 12345)))(CONNECT_DATA =(SID = SIDNAME)));User ID=username;Password=pass;";
}
else if (DataBaseSecureName == "Second")
{
sCon = "Data Source=(DESCRIPTION =(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST = 1.1.1.1)(PORT = 12345)))(CONNECT_DATA =(SID = SIDNAME2)));User ID=username;Password=pass;";
}
OraCon = new OracleConnection(sCon);
}
protected DataTable FillDataTableByParam(OracleParameter[] param)
{
DataTable oDT = new DataTable();
OracleCommand OraCom = new OracleCommand(query, OraCon);
OraCom.Parameters.AddRange(param);
new OracleDataAdapter(OraCom).Fill(oDT);
query = "";
return oDT;
}
}
You can add a method to the Classes/Class i wrote above with logic to return notification on change of connection, and then use it as it pleases you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With