C#串連oracle連接字串

來源:互聯網
上載者:User

標籤:des   blog   http   io   ar   os   使用   for   sp   

/// <summary>
/// Oracle 的資料庫連接字串.
/// </summary>
private const String connString =
@"Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.1.210)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=ORCL)));User Id=TEST;Password=TEST123";

那個 192.168.1.210 是 Oracle 資料庫伺服器的 IP 位址
1521 是 預設的連接埠號碼
SERVICE_NAME=ORCL 意思是 那個 Oracle 資料庫服務是 ORCL
後面就是 使用者名稱 密碼了。

在.NET中使用System.Data.OracleClient庫串連Oracle資料庫的步驟

1.下載並安裝Instant Client

Instant Client可以在Oracle官方網站下載到

對於Windows,Instant Client有三種版本:

1)適用於 Microsoft Windows(32位)的 Instant Client

2)適用於 Microsoft Windows(64位)Itanium的 Instant Client

3)適用於 Microsoft Windows(x64)的 Instant Client

可以通過在我的電腦上單擊右鍵菜單中的屬性中查看到自己電腦的處理器資訊

由於我的電腦是Win7 64位系統,因此我下載了第三個包

下載完畢後,解壓縮並將裡面的bin目錄的地址添加到環境變數path中。

再在環境變數中添加以下屬性:

(我把解壓縮的路徑放到了D盤,解壓縮後的檔案夾名為instantclient_12_1)

NLS_LANG=SIMPLIFIED CHINESE_CHINA.ZHS16GBK

TNS_ADMIN=D:\instantclient_12_1

LD_LIBRARY_PATH=D:\instantclient_12_1

2.使用Xshell4遠端連線安裝有Oracle的RedHat系統

需要輸入以下項:

1)Name:自己起個名字

2)Protocol:選擇SSH

3)Host:目標主機IP

4)Port Number:連接埠號碼(22)

5)登入的使用者名稱/密碼

附:Linux中登入資料庫的命令為

sqlplus 使用者名稱/口令@資料庫名

3.擷取連接字串

尋找ORACLE安裝地址

Linux命令:echo $ORACLE_HOME

tnsnames.ora位於目錄

$ORACLE_HOME\network\admin\

用vim開啟tnsnames.ora後,發現Oracle資料庫的配置如下:

 

xtcsjk =    (DESCRIPTION =        (ADDRESS = (PROTOCOL = TCP)(HOST = 171.0.0.132)(PORT = 1521))        (CONNECT_DATA =            (SERVER = DEDICATED)            (SERVICE_NAME = testdb)        )     )

 

 

 

根據這個檔案,就可以寫出串連這個Oracle資料庫的連接字串了

Data Source=    (DESCRIPTION=        (ADDRESS_LIST=            (ADDRESS=                (PROTOCOL=TCP)                (HOST=171.0.0.132)                (PORT=1521)            )        )        (CONNECT_DATA=            (SERVICE_NAME=TESTDB)        )    );Persist Security Info=True;User Id=使用者名稱;Password=密碼

  

 

4.程式碼

註:運行下面這段代碼,需要

1)手動添加對庫System.Data.OracleClient的引用

2)在程式集→右鍵→屬性→產生中,把目標平台由AnyCPU改為x64

否則會報異常BadImageFormatException(相容性問題)

3)如果沒有完成Instant Client的安裝或環境變數的配置,運行時會報異常資訊:

System.Data.OracleClient 需要 Oracle 用戶端軟體 version 8.1.7 或更高版本

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks; //Method1函數調用的庫,需要手動添加引用using System.Data.OracleClient; namespace OracleTest{    class Program    {        //從Oracle資料庫中讀取時間資訊        static void ReadFromOracleTest()        {            string conn = string.Concat(                @"Data Source=",                @"    (DESCRIPTION=",                @"        (ADDRESS_LIST=",                @"            (ADDRESS=",                @"                (PROTOCOL=TCP)",                 @"                (HOST=171.0.0.132)",                @"                (PORT=1521)",                @"            )",                @"        )",                @"        (CONNECT_DATA=",                @"            (SERVICE_NAME=TESTDB)",                @"        )",                @"    );",                @"Persist Security Info=True;",                @"User Id=使用者名稱;",                @"Password=密碼"                );             //OracleConnection 被標註為已淘汰            OracleConnection oc = new OracleConnection(conn);             try            {                oc.Open();                //OracleCommand 被標註為已淘汰                OracleCommand cmd = oc.CreateCommand();                cmd.CommandText = "select sysdate from dual";                OracleDataReader odr = cmd.ExecuteReader();                while (odr.Read())                {                    Console.WriteLine(odr.GetOracleDateTime(0).ToString());                }            }            catch (Exception ex)            {                Console.WriteLine(ex.Message);            }            finally            {                oc.Close();            }        }         static void Main(string[] args)        {            ReadFromOracleTest();            Console.ReadLine();        }    }}

  

5.運行樣本

C#串連oracle連接字串

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.