您好,登錄后才能下訂單哦!
本文實例總結(jié)了C#連接Oracle的方法。分享給大家供大家參考,具體如下:
一、通過System.Data.OracleClient(需要安裝Oracle客戶端并配置tnsnames.ora)
1. 添加命名空間System.Data.OracleClient引用
2.
using System.Data.OracleClient;
3.
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引用
2.
using System.Data.OracleClient;
3.
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公司的驅(qū)動
1. 添加命名空間System.Data.OracleClient引用
2.
using System.Data.OleDb;
3.
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驅(qū)動
1. 添加命名空間System.Data.OracleClient引用
2.
using System.Data.OleDb;
3.
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操作系統(tǒng)已經(jīng)安裝了微軟公司的Oracle驅(qū)動C:\Program Files\Common Files\System\Ole DB\msdaora.dll
b.該驅(qū)動需要Oracle客戶端的三個文件(oraocixe10.dll、oci.dll、ociw32.dll)放在System32下即可
五、使用ODP連接
1. 下載安裝ODP.NET(http://www.oracle.com/technetwork/developer-tools/visual-studio/downloads/index.html)
2. 安裝完全成后會產(chǎn)生一序列文件。
3. 找到這個安裝目錄,打開文件夾%ORACLE_HOME%\Network\Admin在這個下面建立一個tnsnames.ora的文件,其內(nèi)容可以參考其下的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工具中連接數(shù)據(jù)庫后的TNS描述符中拷過來
4. 引用Oracle.DataAccess命名空間
5.
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(); }
六、使用第三方驅(qū)動
第三方驅(qū)動有 Devart,下載驅(qū)動 http://www.devart.com/dotconnect/oracle/,但是是商業(yè)版,需要購買許可或破解
連接格式 User ID=myUsername;Password=myPassword;Host=ora;Pooling=true;Min Pool Size=0;Max Pool Size=100;Connection Lifetime=0;
1. 引用Devart.Data.Oracle命名空間
2.
using Devart.Data.Oracle;
3.
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(); }
更多關(guān)于C#相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《C#程序設(shè)計之線程使用技巧總結(jié)》、《C#操作Excel技巧總結(jié)》、《C#中XML文件操作技巧匯總》、《C#常見控件用法教程》、《WinForm控件用法總結(jié)》、《C#數(shù)據(jù)結(jié)構(gòu)與算法教程》、《C#數(shù)組操作技巧總結(jié)》及《C#面向?qū)ο蟪绦蛟O(shè)計入門教程》
希望本文所述對大家C#程序設(shè)計有所幫助。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。