C#使用Oracle.ManagedDataAccess.dll組件連接Oracle數(shù)據(jù)庫
一、概述
Oracle Data Provider for .NET, Managed Driver:
Oracle官方的托管數(shù)據(jù)庫訪問組件,單DLL,Oracle.ManagedDataAccess.dll,不安裝Oracle客戶端,不區(qū)分32/64位, 直接引用即可。
與非托管程序為Oracle.DataAccess.dll相比,BulkCopy和CustomType之類的都是不支持的:
1、下載地址
官方下載地址:
http://www.oracle.com/technetwork/database/windows/downloads/index-090165.html (當(dāng)前下版本為64-bit ODAC 12.2c Release 1 (12.2.0.1.0))
nuget:
https://www.nuget.org/packages/Oracle.ManagedDataAccess/ (當(dāng)前下版本為19.7.0)
https://www.nuget.org/packages/Oracle.ManagedDataAccess.EntityFramework/ (當(dāng)前下版本為19.7.0)
下載好后 從這個目錄下 odp.net\managed\common拿出最關(guān)鍵的兩個dll
Oracle.ManagedDataAccess.dll 和 Oracle.ManagedDataAccess.EntityFramework.dll 。
也可以在項目Nuget中直接安裝這兩個包。
注意:如果需要Entity Framework 設(shè)計時特性和其他如TableAdapter向?qū)isual Studio設(shè)計器,可以從https://otn.oracle.com/dotnet下載32位的“Oracle Developer Tools for Visual Studio” 。
此NuGet下載不支持設(shè)計時工具;它只提供運(yùn)行時支持。這個版本的ODP.NET支持Oracle數(shù)據(jù)庫版本11.2或更高版本。
二、配置文件定義數(shù)據(jù)源
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文件,此文件需放置在應(yīng)用程序同目錄:
tnsnames.ora內(nèi)容:
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));
}到此這篇關(guān)于C#使用Oracle.ManagedDataAccess.dll組件連接Oracle數(shù)據(jù)庫的文章就介紹到這了。希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- C# 操作 access 數(shù)據(jù)庫的實例代碼
- c# 使用Entity Framework操作Access數(shù)據(jù)庫的示例
- C#連接Oracle數(shù)據(jù)庫使用Oracle.ManagedDataAccess.dll
- C#實現(xiàn)的ACCESS數(shù)據(jù)庫操作類完整實例
- C#編程實現(xiàn)連接ACCESS數(shù)據(jù)庫實例詳解
- C#動態(tài)創(chuàng)建Access數(shù)據(jù)庫及表的方法
- c#連接access數(shù)據(jù)庫操作類分享
- C# Access數(shù)據(jù)庫增刪查改的簡單方法
- C#操作Access數(shù)據(jù)庫的實現(xiàn)過程(vs2019)
相關(guān)文章
C# 使用 Castle 實現(xiàn) AOP及如何用 Autofac 集成 Castle
這篇文章主要介紹了C# 使用 Castle 實現(xiàn) AOP及如何用 Autofac 集成 Castle,幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下2021-02-02
C#實現(xiàn)動態(tài)創(chuàng)建接口并調(diào)用的實例
這篇文章介紹了C#實現(xiàn)動態(tài)創(chuàng)建接口并調(diào)用,文中通過實例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-11-11
C#使用DevExpress中的SplashScreenManager控件實現(xiàn)啟動閃屏和等待信息窗口
這篇文章介紹了C#使用DevExpress中的SplashScreenManager控件實現(xiàn)啟動閃屏和等待信息窗口的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-05-05
c# 數(shù)據(jù)庫的 sql 參數(shù)封裝類的編寫
c# 數(shù)據(jù)庫的 sql 參數(shù)封裝類的編寫...2007-12-12
淺談C#2.0泛型中的變化:default關(guān)鍵字
下面就詳細(xì)的說明一下。之所以會用到default關(guān)鍵字,是因為需要在不知道類型參數(shù)為值類型還是引用類型的情況下,為對象實例賦初值2013-09-09



