您好,登錄后才能下訂單哦!
本篇內容主要講解“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之類的都是不支持的:
下載好后 從這個目錄下 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或更高版本。
無SQL*Net配置文件tnsnames.ora,直接在C#程序中寫連接字符串
string conString = "User Id=hr; password=hr;Data Source=localhost:1521/orcl; Pooling=false;";
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>
<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數據庫”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。