1. 程式人生 > >C#連線Oracle資料庫的方法(Oracle.DataAccess.Client也叫ODP.net)(重要)

C#連線Oracle資料庫的方法(Oracle.DataAccess.Client也叫ODP.net)(重要)

官方下載地址(ODP.net)(中文):http://www.oracle.com/technetwork/cn/topics/dotnet/downloads/index.html

官方下載地址(ODP.net):http://www.oracle.com/technetwork/topics/dotnet/downloads/index.html

首先介紹下開發環境:WIn10 64bit+Visual Studio 2015+Oracle10ClientWin32(只是客戶端,如果安裝整個資料庫也是可以的)

目前瞭解C#中連線Oracle資料庫的方法有3種,分佈是微軟的System.Data.OracleClient,Oracle的Oracle.DataAccess.Client和Oracle的Oracle.ManagedDataAccess.dll(最優)

1.微軟的System.Data.OracleClient可以直接引用,但是VS會提示“System.Data.OracleClient.OracleConnection”已過時,這表明微軟自己都不建議使用了,所以知道就可以了,不必使用

2.C#使用Oracle.DataAccess.Client也叫ODP.net,他是Oracle提供的資料庫訪問類庫,其功能和效率上都有所保證,它還有一個非常方便特性:在客戶端上,可以不用安裝Oracle客戶端,直接拷貝即可使用。由於微軟在.net framework4中會將System.Data.OracleClient.dll deprecated,而且就訪問效率和速度而言,System.Data.OracleClient.dll與Oracle.DataAccess.dll相比,微軟的確實沒有oracle提供的類庫有優勢,所以我放棄了使用多年的System.Data.OracleClient.dll,取而代之的是odp.net。然而odp.net的優點不止這些,還包括:
1)不在安裝客戶端也能訪問伺服器上的oracle(假設Application Server與DB Server 分開)
2)不需要配置TnsNames.Ora檔案

具體的使用方法請參考這位大俠的  http://blog.csdn.net/rrrrssss00/article/details/7178515/

還有這位大俠的 http://blog.csdn.net/sumirry/article/details/46746331

如果專案要從System.Data.OracleClient.OracleConnection轉Oracle.DataAccess.Client時,只需要在oracle 安裝目錄下 找到 Oracle.DataAccess.dll新增引用,後 using Oracle.DataAccess.Client;
其他的都不用動,即可。
連線字串中 如有 用的是 user=xxx 就改成user id=xxx把原來 Using 的System.Data.OracleClient去掉即可。

3.重點學習最後一種Oracle.ManagedDataAccess.dll,第二種的優點很多,但是也有缺點,就是要區分用區分x86/x64版本。

下載dll和使用方法參考這位大俠的 https://www.cnblogs.com/zouhao/p/9236947.html

 

複製程式碼

複製程式碼

            OracleConnection con = new OracleConnection(ConfigurationManager.ConnectionStrings["OracleConnString"].ToString());
            con.Open();
            OracleCommand cmd = new OracleCommand(cmdString, con);
            OracleDataAdapter oda = new OracleDataAdapter();
            oda.SelectCommand = cmd;
            oda.Fill(ds);
            GridView1.DataSource = ds;
            GridView1.DataBind();

複製程式碼

複製程式碼

 

 

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

 

 

Oracle ODP.NET資料庫訪問連線字串

 

Connection String Attribute

預設值

描述

Connection Lifetime

0

Maximum life time (in seconds) of the connection

當資料庫連線被返回到連線池中時,它的建立時間將與當前時間比較,如果超過了 Connection Lifetime 規定的時間,它將被釋放掉。 為 0 時將被視為最大連線時間。

Connection Timeout

15

Maximum time (in seconds) to wait for a free connection from the pool

Data Source

empty string

Oracle Net Service Name that identifies the database to connect to

DBA Privilege

empty string

Administrative privileges: SYSDBA or SYSOPER

Decr Pool Size

1

Controls the number of connections that are closed when an excessive amount of established connections are unused

Enlist

true

Enables or disables serviced components to automatically enlist in distributed transactions

當此值為 true 時,池中現存的所有資料庫連線將被加入到它的建立執行緒的 Transaction Context 中。如果不存在這個 Transaction Context 則無任何變化。

Incr Pool Size

5

Controls the number of connections that are established when all the connections in the pool are used

Max Pool Size

100

Maximum number of connections in a  pool

Min Pool Size

1

Minimum number of connections in a pool

Password

empty string

Password for the user specified by User Id

Persist Security Info

false

Enables or disables the retrieval of password in the connection string

Pooling

true

Enables or disables connection pooling

Proxy User Id

empty string

User name of the proxy user

Proxy Password

empty string

Password of the proxy user

User Id

empty string

Oracle user name

 

 

 

// C#

...

OracleConnection con = new OracleConnection();

con.ConnectionString = "User Id=scott;Password=tiger;Data Source=oracle;Pooling=true;Enlist=true;Min Pool Size=10;Connection Lifetime=120;Connection Timeout=60;Incr Pool Size=5; Decr Pool Size=2";

con.Open();

...

  

以下網站提供連線字串大全: 

www.ConnectionStrings.com