There is a problem with creating a connection to Oracle. The variable is created:

private OracleConnection conn; 

Then connects:

 conn = new OracleConnection(connection_string); 

System.TypeInitializationException: 'An initializer of the type "Oracle.DataAccess.Client.OracleConnection" threw an exception.'

Connection is made using the library using Oracle.DataAccess.Client; I have never worked with it, I usually use using System.Data.OracleClient What is wrong with me, please tell me? connectionstring is valid.

  • And the exception is what? - Viktorov
  • @lDrakonl is the error text. The provider is not compatible with the version of Oracle client. - Andrei
  • @lDrakonl I understand you need to replace DataAccess.Client with OracleClient? - Andrei
  • Andrew, build a mini sample code that causes an error and add it to the question. Or tell me what my answer does not suit you - Viktorov
  • Indicate in the question what version of client is used and ODP.Net - default locale

1 answer 1

Connect with Oracle and query.

 using System.IO; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Oracle.DataAccess.Client; using Oracle.DataAccess.Types; namespace HelloWorld2 { class Program { static void Main(string[] args) { string oracleConnectionString = "Data Source=DEV_V;User id=snapshoter;Password=snapshoter;"; OracleConnection connection = new OracleConnection(oracleConnectionString); try { // откроем соединение connection.Open(); OracleCommand oracleCommand = new OracleCommand(); oracleCommand.Connection = connection; // запрос на создание снапшота. У пользователя должны быть необходимые права oracleCommand.CommandText = "select 1 as num from dual"; OracleDataReader reader = oracleCommand.ExecuteReader(); reader.Read(); int num = 0; //получим результат запроса num = reader.GetInt32(0); Console.WriteLine("num = " + num); } catch (OracleException ex) { Console.WriteLine(ex.Message); } finally { connection.Close(); connection.Dispose(); } Console.ReadKey(); } } }