我正在开发一个asp.net web api应用程序,我正在尝试在c#中获取oracle数据库更改通知。但是当我初始化1 oracle依赖时,一切都按预期工作但当我尝试从2个不同的oracle数据库初始化2 oracle依赖时,我收到错误“' 此操作需要对象使用相同的连接 '“
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());
答案 0 :(得分:4)
首先,本分步指南可能会解决您的问题: Oracle Dependency Guide
第二,这也是您可以用来完成工作的另一项工作。
是的,可以。您的数据库层应包含2个将连接到相应数据库的类。另外,请勿使用ExecuteReader
,而应使用DataAdapter
,因为它可以处理Connection.Open(),Close()和Dispose()本身。
例如
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;
}
}
但是您还可以在数据库层中保留1个类,使其可用于2个数据库,这看起来像这样:
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;
}
}
您可以向我在上面编写的Classes / Class中添加一种方法,该方法具有逻辑以返回连接更改通知,然后随便使用它。