a question has arisen whether it is possible to load the dll based on the version
user-selected
Yes it is possible, however if you load a dll by reflection
, everything you do, will have to be done by reflection
.
Example:
var assembly = Assembly.LoadFrom(@"C:\Temp\Oracle.ManagedDataAccess.dll");
var oracleConnectionType = assembly.GetType("Oracle.ManagedDataAccess.Client.OracleConnection");
var connectionInstance = assembly.CreateInstance(oracleConnectionType.FullName);
and so it goes... this ends up making your code very complex and difficult to maintain.
Is there any way to reference them dynamically based on
radio button where the user would select which version to use?
Well, it is possible to load both dll at the same time and create specific methods for each of them.
The first step is to reference them in your project, but beware, they cannot have the same name.
The second step is to define an alias for each of them:
In one of them define the alias for OnzeG
and to the other DozeC
.
The third step is to reference these aliases in your code, above the usings
:
extern alias DozenoveI;
extern alias OnzeG;
After that, create your methods referencing all types with their specific aliases, putting them in front of the method as follows
alias::Type
Example:
public DataTable ExecutarCom11g(string connectionString, string query)
{
using (OnzeG::Oracle.ManagedDataAccess.Client.OracleConnection conn = new OnzeG::Oracle.ManagedDataAccess.Client.OracleConnection(connectionString))
{
conn.Open();
OnzeG::Oracle.ManagedDataAccess.Client.OracleCommand oracleCommand = conn.CreateCommand();
oracleCommand.CommandText = query;
DataTable dataTable = new DataTable();
OnzeG::Oracle.ManagedDataAccess.Client.OracleDataReader reader = oracleCommand.ExecuteReader();
dataTable.Load(reader);
return dataTable;
}
}
public DataTable ExecutarCom12c(string connectionString, string query)
{
using (DozeC::Oracle.ManagedDataAccess.Client.OracleConnection conn = new DozeC::Oracle.ManagedDataAccess.Client.OracleConnection(connectionString))
{
conn.Open();
DozeC::Oracle.ManagedDataAccess.Client.OracleCommand oracleCommand = conn.CreateCommand();
oracleCommand.CommandText = query;
DataTable dataTable = new DataTable();
DozeC::Oracle.ManagedDataAccess.Client.OracleDataReader reader = oracleCommand.ExecuteReader();
dataTable.Load(reader);
return dataTable;
}
}
After that it is only encapsulate the methods in a class and call them when it is convenient:
string connectionString = "Data Source=banco;User ID=usuario;Password=senha;";
string query = "select 1 from dual";
BancoDeDados conectorBancoDeDados = new BancoDeDados();
var resultado11g = conectorBancoDeDados.ExecutarCom11g(connectionString, query);
var resultado12i = conectorBancoDeDados.ExecutarCom12c(connectionString, query);
Fantastic, I did not know it was possible to define alias for references, thank you very much worked!
– Lodi