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.
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.
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.
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 :
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(); } } } }
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With