我正在开发一个asp.net web api应用程序,我正在尝试在c#中获取oracle数据库更改通知.但是当我正在初始化1 oracle依赖时,一切都按预期工作但当我尝试从2个不同的oracle数据库初始化2 oracle依赖时,我收到错误“’此操...
我正在开发一个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());
解决方法:
首先,此分步指南可以解决您的问题:
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;
}
}
您可以使用逻辑向上面编写的类/类添加一个方法,以返回更改连接的通知,然后随意使用它.
本文标题为:c# – 我们可以在一个asp.net应用程序中从2个不同的数据库中获得2个不同的Oracle依赖关系
基础教程推荐
- DataTable的AcceptChanges()和RejectChanges()方法介绍并实现DataGridView数据增、删、改 2023-05-22
- C# .NET 中的缓存实现详情 2023-05-05
- C#中使用FilleStream实现视频文件的复制功能 2023-01-28
- C#异步迭代IAsyncEnumerable应用实现 2023-04-22
- 小白2分钟学会Visual Studio如何将引用包打包到NuGet上 2023-04-28
- C#实现输入法功能详解 2022-11-02
- .net core 部署 centos 7.6 2023-09-27
- C#设计模式之Visitor访问者模式解决长隆欢乐世界问题实例 2022-11-15
- WPF实现倒计时转场动画效果 2023-06-27
- C#使用泛型队列Queue实现生产消费模式 2023-07-05