c# – 我们可以在一个asp.net应用程序中从2个不同的数据库中获得2个不同的Oracle依赖关系

我正在开发一个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依赖关系

基础教程推荐