我正在编写一个使用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}
}
}
发布于 2014-01-29 05:20:03
在查看了不同的选项之后,一种可能的方法是使用c# dynamic关键字。它将允许我们在运行时获得所需的类型,并且它可以修复编译错误。在相同的行中,我们可以使用ObjectHandle objObjectHandle = Activator.CreateInstance
在运行时创建实例,并可以使用using函数来使用它。
Using Activator.CreateInstance to get the object type at runtime.
ObjectHandle objObjectHandle = Activator.CreateInstance("Npgsql","Npgsql.NpgsqlDataAdapter");
要使用dynamic关键字,需要使用Microsoft.CSharp.dll版本4,将其引用添加到项目文件中。我们正在研究的其他解决方案是为这两种类型的数据库创建一个库项目并建立一个标准对象模型。
https://stackoverflow.com/questions/21400970
复制相似问题