C#串連Oracle的執行個體教程

來源:互聯網
上載者:User
這篇文章主要介紹了C#串連Oracle的方法,結合執行個體形式總結分析了幾種常見的C#串連Oracle資料庫的操作技巧與相關注意事項,需要的朋友可以參考下

本文執行個體總結了C#串連Oracle的方法。分享給大家供大家參考,具體如下:

一、通過System.Data.OracleClient(需要安裝Oracle用戶端並配置tnsnames.ora)

1. 添加命名空間System.Data.OracleClient引用

 using System.Data.OracleClient;
string connString = "User ID=IFSAPP;Password=IFSAPP;Data Source=RACE;";OracleConnection conn = new OracleConnection(connString);try{  conn.Open();  MessageBox.Show(conn.State.ToString());}catch (Exception ex){  ShowErrorMessage(ex.Message.ToString());}finally{  conn.Close();}

二、通過System.Data.OracleClient(需要安裝Oracle用戶端不需配置tnsnames.ora)

1. 添加命名空間System.Data.OracleClient引用

 using System.Data.OracleClient;
string connString = "User ID=IFSAPP;Password=IFSAPP;Data Source=(DESCRIPTION = (ADDRESS_LIST= (ADDRESS = (PROTOCOL = TCP)(HOST = 127.0.0.1)(PORT = 1521))) (CONNECT_DATA = (SERVICE_NAME = RACE)))";OracleConnection conn = new OracleConnection(connString);try{  conn.Open();  MessageBox.Show(conn.State.ToString());}catch (Exception ex){  ShowErrorMessage(ex.Message.ToString());}finally{  conn.Close();}

三、通過System.Data.OleDb和Oracle公司的驅動

1. 添加命名空間System.Data.OracleClient引用

using System.Data.OleDb;
string connString = "Provider=OraOLEDB.Oracle.1;User ID=IFSAPP;Password=IFSAPP;Data Source=(DESCRIPTION = (ADDRESS_LIST= (ADDRESS = (PROTOCOL = TCP)(HOST = 127.0.0.1)(PORT = 1521))) (CONNECT_DATA = (SERVICE_NAME = RACE)))";OleDbConnection conn = new OleDbConnection(connString);try{  conn.Open();  MessageBox.Show(conn.State.ToString());}catch (Exception ex){  ShowErrorMessage(ex.Message.ToString());}finally{  conn.Close();}

四、通過System.Data.OleDb和微軟公司的Oracle驅動

1. 添加命名空間System.Data.OracleClient引用

using System.Data.OleDb;
string connString = "Provider=MSDAORA.1;User ID=IFSAPP;Password=IFSAPP;Data Source=(DESCRIPTION = (ADDRESS_LIST= (ADDRESS = (PROTOCOL = TCP)(HOST = 127.0.0.1)(PORT = 1521))) (CONNECT_DATA = (SERVICE_NAME = RACE)))";OleDbConnection cnn = new OleDbConnection(connString);try{  conn.Open();  MessageBox.Show(conn.State.ToString());}catch (Exception ex){  ShowErrorMessage(ex.Message.ToString());}finally{  conn.Close();}

備忘:

a.XP作業系統已經安裝了微軟公司的Oracle驅動C:\Program Files\Common Files\System\Ole DB\msdaora.dll

b.該驅動需要Oracle用戶端的三個檔案(oraocixe10.dll、oci.dll、ociw32.dll)放在System32下即可

五、使用ODP串連

1. 下載安裝ODP.NET(www.oracle.com/technetwork/developer-tools/visual-studio/downloads/index.html)

2. 安裝完全成後會產生一序列檔案。

3. 找到這個安裝目錄,開啟檔案夾%ORACLE_HOME%\Network\Admin在這個下面建立一個tnsnames.ora的檔案,其內容可以參考其下的Sample目錄下面的配置

Oracle.RACE =(DESCRIPTION=  (ADDRESS_LIST=   (ADDRESS=    (PROTOCOL=TCP)    (HOST=127.0.0.1)    (PORT=1521)   )  )  (CONNECT_DATA=   (SID=RACE)   (SERVER=DEDICATED)  ))

Oracle.RACE為連接字串名稱,可以隨便取。等號後面的字串可以在Enterprise Manager Console工具中串連資料庫後的TNS描述符中拷過來

4. 引用Oracle.DataAccess命名空間

using Oracle.DataAccess.Client;

6. 範例程式碼:

string connString = "DATA SOURCE=Oracle.RACE;PERSIST SECURITY INFO=True;USER ID=IFSAPP;password=IFSAPP";OracleConnection conn = new OracleConnection(connString);try{  conn.Open();  OracleCommand cmd = new OracleCommand(cmdText,conn);  OracleDataReader reader = cmd.ExecuteReader();  this.DataGridView1.DataSource = reader;  this.DataGridView1.DataBind();}catch (Exception ex){  ShowErrorMessage(ex.Message.ToString());}finally{  conn.Close();}

六、使用第三方驅動

第三方驅動有 Devart,下載驅動www.devart.com/dotconnect/oracle/,但是是商業版,需要購買許可或破解

串連格式 User ID=myUsername;Password=myPassword;Host=ora;Pooling=true;Min Pool Size=0;Max Pool Size=100;Connection Lifetime=0;

1. 引用Devart.Data.Oracle命名空間

using Devart.Data.Oracle;
OracleConnection conn = new OracleConnection();conn.ConnectionString = "";conn.Unicode = true;conn.UserId = "IFSAPP";conn.Password = "IFSAPP";conn.Port = 1521;conn.Server = "127.0.0.1";conn.Sid = "RACE";try{  conn.Open();  //execute queries, etc}catch (Exception ex){  ShowErrorMessage(ex.Message.ToString());}finally{  conn.Close();}
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.