Connecting to Oracle using Oracle.ManagedDataAccess

无人久伴 提交于 2019-12-03 23:28:42

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 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

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();
    }
}

You can make a separate class file returning the connectionstring like this -

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();
oracon.Open();

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;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!