在dotnet mvc应用程序中连接到Oracle数据库 [英] Connect to Oracle database in dotnet mvc application

查看:87
本文介绍了在dotnet mvc应用程序中连接到Oracle数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Oracle发布了Beta版用于dotnet核心2的驱动程序ODP (最后!).但是我不能使它工作.有人做过吗?如果是,请向我发送代码或帮助我修复此\ o

Oracle released a beta version driver ODP for dotnet core 2(Finally!). But I can't make it work. Does anybody did it? If yes, please send me the code or help me fixing this one \o

有用:我正在使用Visual Code,项目"dotnet new mvc"(.net 2),并且我通过Nuget添加软件包工具(CTRL + P,...)安装了Oracle.ManagedDataAccess.Client. 这是我的代码:

Useful: I am using Visual Code, project "dotnet new mvc" (.net 2) and I installed Oracle.ManagedDataAccess.Client via Nuget Add Package facility (CTRL + P, ...) Here my code:

public static OracleConnection AbrirSigmaUser(AutenticacaoModel autenticacao)
        {
            try
            {

                string _connectionString;

                _connectionString = "Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.0.15)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=databaseName)));User Id=UserName;Password=***;";


                OracleConnection conexao = new OracleConnection();
                conexao.ConnectionString = _connectionString;

                //right here the program exit
                conexao.Open();             




                return conexao;
            }
            catch (System.Exception ex)
            {               

                throw new Exception("Não foi possível conectar ao banco de dados!" + "\nErro: " + ex.Message);
            }
        }

编译器抛出了许多有关缺少dll的异常,因此我通过Nuget Add Package工具安装了它们:

The compiler was throwing a lot of exceptions about missing dlls, so I installed them via Nuget Add Package facility:

//required for connection
using Oracle.ManagedDataAccess.Client;
using System.Configuration;
using System.Security.Permissions;
using System.Security.Principal;

在添加所有要求的dll之后,程序将转到conexao.Open(),再也不会返回,并抛出未处理的异常:

After adding all dlls asked, the program goes to conexao.Open() and never comes back, throwing an unhandled exception:

Unhandled Exception: System.TypeLoadException: Could not load type 'System.Security.Principal.WindowsImpersonationContext' from assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.
   at OracleInternal.ConnectionPool.PoolManager`3.CreateNewPRThreadFunc(Object state)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadPoolWorkQueue.Dispatch()
The program '[8860] Sensatta-Analytcs.dll' has exited with code 0 (0x0).

建立一个简单的连接真的需要所有这些吗?

Is all of it really needed to make a simple connection?

如果要弄清楚我的问题(例如代码示例),请告诉我!

If there's something missing(e.g code samples) in order to figure out my issue, just tell me!

推荐答案

正如@Lesiak所说,我使用了错误的软件包(正确的软件包是Oracle.ManagedDataAccess.Core.dll).但这是其他人也可能犯的错误.因为当您从 Oracle Link 下载zip文件时, dll名称是Oracle.ManagedDataAccess.dll而不是Oracle.ManagedDataAccess.Core.dll,这很容易出错.

As @Lesiak stated, I used the wrong package (the right one is Oracle.ManagedDataAccess.Core.dll). But that is an error someone else might fall in too. Because when you download the zip file from Oracle Link the dll name is Oracle.ManagedDataAccess.dll instead of Oracle.ManagedDataAccess.Core.dll that's error prone.

这篇关于在dotnet mvc应用程序中连接到Oracle数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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