I am using Oracle.ManagedDataAccess Nuget Package Version 12.1.022 in my C# (.NET 4.0) project. The package automatically creates entries in the app.config file. How can I r
You can make a separate class file returning the connectionstring like this -
public class OracleDbConnection
{
public static OracleConnection GetConnection()
{
const string connectionString = "Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=127.0.0.1)(PORT=1521)) (CONNECT_DATA=(SERVICE_NAME=PROD))); User Id=userId;Password=password;";
var connection=new OracleConnection(connectionString);
return connection;
}
}
Then you can call it like this where you need to access oracle db-
var oracleConnection = OracleDbConnection.GetConnection();
oracleConnection.Open();
Typically, you would refer to the alias in a standard connection string:
<connectionStrings>
<add name="MyConnection" connectionString="Data Source=MyDataSource;User Id=scott;Password=tiger;"/>
</connectionStrings>
Then you would use the standard method for retrieving the string:
ConfigurationManager.ConnectionStrings["MyConnection"].ConnectionString;
Also keep in mind that the alias in the data source section is optional. You can embed the descriptor directly in the connection string:
<connectionStrings>
<add name="MyConnection" connectionString="Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=100.100.100.100)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=myservice.com)));User Id=scott;Password=tiger;"/>
</connectionStrings>
You can also refer to aliases in a tnsnames.ora file. By default the driver looks for a tnsnames.ora in the exe folder, a directory specified in a TNS_ADMIN environment variable, or the TNS_ADMIN config variable:
http://docs.oracle.com/cd/E48297_01/doc/win.121/e41125/featConfig.htm#autoId6 http://docs.oracle.com/cd/E48297_01/doc/win.121/e41125/featConfig.htm#autoId7
Using the package Formo, I created following method to get the MyDataSource
string from app.config
file
using Configuration = Formo.Configuration;
.......
/// <summary>
/// Gets the data source from app.config file
/// </summary>
/// <returns></returns>
public string GetMyDataSource()
{
dynamic config = new Configuration();
return config.MyDataSource;
}
You can refer to the already defined datasource in the connection string
<oracle.manageddataaccess.client>
<version number="*">
<dataSources>
<dataSource alias="MyDataSource" descriptor="(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=100.100.100.100)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=myservice.com)))" />
</dataSources>
</version>
</oracle.manageddataaccess.client>
for instance, in this specific case:
public class OracleDBManager
{
private OracleConnection _con;
private const string connectionString = "User Id={0};Password={1};Data Source=MyDataSource;";
private const string OracleDBUser = "exampleUser";
private const string OracleDBPassword = "examplePassword";
public OracleDBManager()
{
InitializeDBConnection();
}
~OracleDBManager()
{
if (_con != null)
{
_con.Close();
_con.Dispose();
_con = null;
}
}
private void InitializeDBConnection()
{
_con = new OracleConnection();
_con.ConnectionString = string.Format(connectionString, OracleDBUser, OracleDBPassword);
_con.Open();
}
}