您好,登錄后才能下訂單哦!
這篇文章主要介紹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操作系統(tǒng)已經安裝了微軟公司的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工具中連接數(shù)據(jù)庫后的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/,但是是商業(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命名空間
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的方法”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業(yè)資訊頻道!
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經查實,將立刻刪除涉嫌侵權內容。