C#使用托管程序连接Oracle数据库(Oracle.ManagedDataAccess.dll)

2019/08/22 12:32
阅读数 1.3K

一、概述

Oracle Data Provider for  .NET, Managed Driver:

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

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

下载地址

官方下载地址:

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.3.1)

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

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

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

注意:如果需要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

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\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>

使用

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

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>
展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部