如何从.Net Core连接到Oracle数据库连接 [英] How to connect to an Oracle database Connection from .Net Core

查看:1381
本文介绍了如何从.Net Core连接到Oracle数据库连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在.netCore库中,我想连接到Oracle数据库.我有什么办法可以做到吗?

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

我尝试了另一篇SO帖子上的建议,但是它不起作用,也许自那以后被删除了?如您在我的project.json中所看到的,我正在尝试使用"net461".

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

我目前正在尝试通过老式的ADO.Net使用Oracle.ManagedDataAccess.Client.我也知道Oracle尚未购买.netCore连接器.但是即使到那里我都无法使用它,也很难包含 System.Data ,每当我尝试添加它时都会出错.

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.

我的project.json看起来像这样:

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

但是上面的代码由于没有System.Data而无法编译,因此我很难导入.

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.

推荐答案

Oracle发布了 nuget上.NET Core的官方数据提供程序.

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

基本上,您可以像使用正式的.NET System.Data.SqlClient(很容易找到它的在线教程)一样使用它,只需将代码中的SqlConnection替换为OracleConnection,将SqlCommand替换为OracleCommand.

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.

这篇关于如何从.Net Core连接到Oracle数据库连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆