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

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

问题描述

Oracle 发布了测试版用于 dotnet core 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 添加包工具安装了 Oracle.ManagedDataAccess.Client(CTRL + P, ...)这是我的代码:

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!" + "
Erro: " + ex.Message);
            }
        }

编译器抛出了很多关于缺少 dll 的异常,所以我通过 Nuget 添加包工具安装它们:

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天全站免登陆