C#使用Oracle.ManagedDataAccess.dll组件连接Oracle数据库

2022-11-13 08:11:06 数据库 连接 组件

一、概述

Oracle Data Provider for  .NET, Managed Driver:

oracle官方的托管数据库访问组件,单DLL,Oracle.ManagedDataAccess.dll,不安装Oracle客户端,不区分32/64位, 直接引用即可。

与非托管程序为Oracle.DataAccess.dll相比,BulkCopy和CustomType之类的都是不支持的:

1、下载地址

官方下载地址:

Http://www.oracle.com/technetwork/database/windows/downloads/index-090165.html  (当前下版本为64-bit ODAC 12.2c Release 1 (12.2.0.1.0))

nuget:

https://www.nuget.org/packages/Oracle.ManagedDataAccess/ (当前下版本为19.7.0)

https://www.nuget.org/packages/Oracle.ManagedDataAccess.EntityFramework/ (当前下版本为19.7.0)

下载好后  从这个目录下 odp.net\managed\common拿出最关键的两个dll

Oracle.ManagedDataAccess.dll 和 Oracle.ManagedDataAccess.EntityFramework.dll 。

也可以在项目Nuget中直接安装这两个包。

注意:如果需要Entity Framework 设计时特性和其他如TableAdapter向导Visual Studio设计器,可以从https://otn.oracle.com/dotnet下载32位的“Oracle Developer Tools for Visual Studio” 。

此NuGet下载不支持设计时工具;它只提供运行时支持。这个版本的ODP.NET支持Oracle数据库版本11.2或更高版本。

二、配置文件定义数据源

1、无配置文件情况,直接连接DB

sql*Net配置文件tnsnames.ora,直接在C#程序中写连接字符串

string conString = "User Id=hr; passWord=hr;Data Source=localhost:1521/orcl; Pooling=false;";

2、使用配置文件方式:

1、使用 tnsnames.ora文件,此文件需放置在应用程序同目录:

tnsnames.ora内容:

ORCL =
              (DESCRIPTION =
                (ADDRESS = (PROTOCOL = tcp)(HOST = localhost)(PORT = 1521))
                (CONNECT_DATA =
                  (SERVER = DEDICATED)
                  (SERVICE_NAME = orcl)
                )
              )

2、使用app.config或\WEB.config配置文件定义别名(无tnsnames.ora文件)

<oracle.manageddataaccess.client>
  <version number="*">
    <dataSources>
       <dataSource alias="orcl" descriptor="(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=localhost)(PORT=1521))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME = orcl)))"/>
     </dataSources>
   </version>
 </oracle.manageddataaccess.client>

3、使用 TNS_ADMIN目录:

<oracle.manageddataaccess.client>
  <version number="*">
    <settings>
           <setting name="TNS_ADMIN" value="C:\app\oracle\product\12.1.0\client_1\Network\Admin\"/>  
     </settings>
   </version>
 </oracle.manageddataaccess.client>

3、完整的app/web.config 文件:

<configuration>
    <configSections>
    <section name="oracle.manageddataaccess.client"
      type="OracleInternal.Common.ODPMSectionHandler, Oracle.ManagedDataAccess, Version=4.122.19.1, Culture=neutral, PublicKeyToken=89b483f429c47342"/>
    </configSections>
    <system.data>
        <DbProviderFactories>
            <remove invariant="Oracle.ManagedDataAccess.Client"/>
            <add name="ODP.NET, Managed Driver" invariant="Oracle.ManagedDataAccess.Client" description="Oracle Data Provider for .NET, Managed Driver"
                type="Oracle.ManagedDataAccess.Client.OracleClientFactory, Oracle.ManagedDataAccess, Version=4.122.19.1, Culture=neutral, PublicKeyToken=89b483f429c47342"/>
        </DbProviderFactories>
    </system.data>
    <runtime>
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
            <dependentAssembly>
                <publisherPolicy apply="no"/>
                <assemblyIdentity name="Oracle.ManagedDataAccess" publicKeyToken="89b483f429c47342" culture="neutral"/>
                <bindingRedirect oldVersion="4.121.0.0 - 4.65535.65535.65535" newVersion="4.122.19.1"/>
            </dependentAssembly>
        </assemblyBinding>
    </runtime>
    <oracle.manageddataaccess.client>
        <version number="*">
            <dataSources>
                <dataSource alias="SampleDataSource"
                    descriptor="(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=localhost)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=ORCL))) "/>
            </dataSources>
        </version>
    </oracle.manageddataaccess.client>
</configuration>

三、使用

string conString = "User Id=hr; password=hr; Data Source=orcl;Pooling=false; ";

OracleConnection con = new OracleConnection();
con.ConnectionString = conString;
con.Open();

OracleCommand cmd = con.CreateCommand();
cmd.CommandText = "select first_name from employees where department_id = 60";

OracleDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
    string conString = "User Id=hr; password=hr; Data Source=orcl;Pooling=false; ";

    OracleConnection con = new OracleConnection();
    con.ConnectionString = conString;
    con.Open();

    OracleCommand cmd = con.CreateCommand();
    cmd.CommandText = "select first_name from employees where department_id = 60";

    OracleDataReader reader = cmd.ExecuteReader();
    while (reader.Read())
    {
        Console.WriteLine("Employee Name: " + reader.GetString(0));
    }
    Console.WriteLine("Employee Name: " + reader.GetString(0));
}

到此这篇关于C#使用Oracle.ManagedDataAccess.dll组件连接Oracle数据库的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持。

相关文章