這篇文章主要介紹了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(); }
以上是C#連接Oracle的實例教程的詳細內容。更多資訊請關注PHP中文網其他相關文章!