Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to connect to an Oracle database Connection from .Net Core

Tags:

Within a .netCore library I want to connect to an Oracle database. Is there any way I can do that yet?

I have tried the suggestions on another SO post, but it doesn't work, perhaps removed since? As you can see in my project.json, I'm trying to use "net461".

I'm currently trying using Oracle.ManagedDataAccess.Client via old fashioned ADO.Net. I also know that Oracle haven't bought out a .netCore connector yet. But even there I can't get it to work, it struggles to get the System.Data included, it errors whenever I try to add it.

My project.json looks like this:

{   "version": "1.0.0-*",    "dependencies": {     "NETStandard.Library": "1.6.0",     "Oracle.ManagedDataAccess": "12.1.24160719",   },    "frameworks": {     "netstandard1.6": {       "imports": [         "dnxcore50",         "net461"       ]     }   } } 

This is how I was trying to do it at the moment.

using Oracle.ManagedDataAccess.Client;  public class MyRepository {     public string GetServerVersion()     {         var _db = new OracleConnection("User Id=myUser;Password=myPassword;Data Source=MyOracleConnection");          var serverVersion = _db.ServerVersion;         return serverVersion;     } } 

However the above doesn't compile as it doesn't have System.Data, which I'm struggling to import.

I'm not entrenched on any particular way of doing it, I just want the best reasonable option at this point in time.

like image 644
David C Avatar asked Jan 04 '17 08:01

David C


People also ask

How do I connect to an Oracle database remotely?

Connecting remotely means running the SQL Command Line (or any other Oracle command-line utility) on a computer other than the Oracle Database XE host computer, and then initiating a database connection from the SQL Command Line (or other utility) over the network.

What is Oracle Manageddataaccess core?

Oracle Data Provider for . NET (ODP.NET) Core is an ADO.NET driver that provides fast data access from Microsoft . NET Core clients to Oracle databases.


2 Answers

Beta release .Net Core Managed driver released by Oracle at the end of January 2018 http://www.oracle.com/technetwork/topics/dotnet/downloads/net-downloads-160392.html. Supported platfom mentionet in doc is now Win and Linux.

Nuget: https://www.nuget.org/packages/Oracle.ManagedDataAccess.Core

Other old alternatives with standart/instant Oracle clients :

  • for .Net Core 2.0 I recomend to use ericmend oracleClientCore-2.0 : https://github.com/ericmend/oracleClientCore-2.0. Nuget: dotNetCore.Data.OracleClient I used it succesfully in Win and Linux platform. There is my small sample
  • alternativelly System.Data.OracleClient works for 2.0 too - see @Owen post. But I test it only in Win platform
  • for .Net Core >= 1.0 you can use unofficial LinqDan Oracle client for .NET Core based on Mono's Oracle client https://github.com/LinqDan/oracleclientcore Nuget: Mono.Data.OracleClientCore.

my TestCore.csproj for last alternative :

<Project Sdk="Microsoft.NET.Sdk">   <PropertyGroup>     <OutputType>Exe</OutputType>     <TargetFramework>netcoreapp1.0</TargetFramework>   </PropertyGroup>   <ItemGroup>     <PackageReference Include="Mono.Data.OracleClientCore" Version="1.0.0" />   </ItemGroup> </Project> 

My program.cs:

using System; using System.Data.OracleClient;  namespace TestCore {     class Program     {         static void Main(string[] args)         {             Console.WriteLine("Starting.\r\n");                                   using (var _db = new OracleConnection("User Id=myUser;Password=myPassword;Data Source=MyOracleConnection"))             {                 Console.WriteLine("Open connection...");                 _db.Open();                 Console.WriteLine(  "Connected to:" +_db.ServerVersion);                 Console.WriteLine("\r\nDone. Press key for exit");                 Console.ReadKey();             }                    }     } } 
like image 147
Dubo Avatar answered Sep 27 '22 23:09

Dubo


Oracle published the official Data Provider for .NET Core on nuget.

Here is a basic example to show how to use it:

using Oracle.ManagedDataAccess.Client;  public void Execute(string queryString, string connectionString) {     using (OracleConnection connection = new OracleConnection(connectionString))     {         OracleCommand command = new OracleCommand(queryString, connection);         command.Connection.Open();         command.ExecuteNonQuery();     } } 

Basically you can use it exactly like the official .NET System.Data.SqlClient (easy to find online tutorials for this) and just replace everywhere in the code SqlConnection with OracleConnection and SqlCommand with OracleCommand.

like image 26
RemiGaudin Avatar answered Sep 27 '22 23:09

RemiGaudin