C#如何使用Oracle.ManagedDataAccess.dll组件连接Oracle数据库
本篇内容主要讲解“C#如何使用Oracle.ManagedDataAccess.dll组件连接Oracle数据库”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“C#如何使用Oracle.ManagedDataAccess.dll组件连接Oracle数据库”吧!
一、概述
Oracle Data Provider for .NET, Managed Driver:
Oracle官方的托管数据库访问组件,单DLL,Oracle.ManagedDataAccess.dll,不安装Oracle客户端,不区分32/64位, 直接引用即可。
与非托管程序为Oracle.DataAccess.dll相比,BulkCopy和CustomType之类的都是不支持的:
1、下载
下载好后 从这个目录下 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数据库”有了更深的了解,不妨来实际操作一番吧!这里是蜗牛博客网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:niceseo99@gmail.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。
评论