Can you change which provider OracleConnection uses by default?

126 Views Asked by At

I have an application that uses System.Data.OracleClient.OracleConnection (yes I know this is obsolete) without specifying a provider. It appears to be using MSDORA by default. Can I change the provider without adding a provider on the connection string? eg in the app.config file or a machine wide setting?

1

There are 1 best solutions below

0
Ninos Yomo On

A more optimal option would be to use ODP.NET

To implement this, start by adding a reference to Oracle.DataAccess. Then, add the following two lines before your public declaration:

using Oracle.DataAccess.Client; // ODP.NET Oracle managed provider
using Oracle.DataAccess.Types; 

This should allow you to use ODP.NET as follows:

string oradb = "Data Source=mysrc;User Id=id;Password=password;";
OracleConnection conn = new OracleConnection(oradb);  // C#
conn.Open(); 
OracleCommand cmd = new OracleCommand();
cmd.Connection = conn;
cmd.CommandText = "my query";
cmd.CommandType = CommandType.Text; 
OracleDataReader dr = cmd.ExecuteReader();
dr.Read();
label1.Text = dr.GetString(0); //Assuming the label already exists
conn.Dispose();

For more help, try following Oracle's official guide found here