0

我正在编写使用 postgres 数据库的代码。这段代码已经使用了 oracle 特定的函数,我添加了一些函数来访问 postgres 表。需要进行仅使用 postgres 功能的构建,不需要 oracle 代码行,因此不需要 ODAC。

http://www.oracle.com/technetwork/topics/dotnet/utilsoft-086879.html

有一些选项,例如为 c# 添加条件编译,以便不为 postgres 执行 oracle 特定代码。这样做不会在编译的 dll 中添加基于 ODAC 的代码部分,因此不需要 ODAC,但这是唯一的方法。正如我所说,应用程序中有很多项目,进行条件编译会创建很多额外的代码。

或者还有什么可以避免 ODAC 许可的其他替代方案。

谢谢,

function GetData()
{
 if(Postgres flag){
// code for using OracleClientFactory and initlizing the oraclecommand

}
else if(Oracle flag)
{
   // code for using postgres and initlizing the its command object
}


}
 // code to assign query to command object and execute the query
// return the resluts

catch (){}
finally{//dispoing the objects}

}

}

4

1 回答 1

0

在研究了不同的选项之后,一种可能的方法是使用 c# 动态关键字。它将允许我们在运行时获得所需的类型,并且可以修复编译错误。在同一行中,我们可以使用 ObjectHandle objObjectHandle = Activator.CreateInstance在运行时创建实例,并可以使用 unwrap 函数来使用它。

Using Activator.CreateInstance to get the object type at runtime.

ObjectHandle objObjectHandle = Activator.CreateInstance("Npgsql","Npgsql.NpgsqlDataAdapter");

要使用动态关键字,需要使用 Microsoft.CSharp.dll 版本 4,在项目文件中添加其引用。我们正在寻找其他解决方案来创建库项目并为两种类型的数据库制作标准对象模型。

于 2014-01-29T13:20:03.027 回答