1. 程式人生 > >vs2015連線oracle(11g)的方法

vs2015連線oracle(11g)的方法

1.下載Oracle Developer Tools for Visual Studio 2015 ,網址如下。

http://www.oracle.com/technetwork/topics/dotnet/downloads/odacmsidownload-2745497.html

2.安裝好後參照oracle的安裝目錄下的 tnsnames.ora檔案的最後一段修改Oracle Developer Tools for Visual Studio 2015安裝目錄下的 tnsnames.ora檔案的最後一 段(直接複製)我的如下ORCL =   (DESCRIPTION =     (ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521))     (CONNECT_DATA =       (SERVER = DEDICATED)       (SERVICE_NAME = orcl)     )       F:\app\sky\product\11.2.0\dbhome_1\NETWORK\ADMIN

3.開啟vs會發現工具選項裡面多了sqlplus等roacle的內容,在專案的引用上點選右鍵新增引用-擴充套件-選擇Oracle.ManageDataAccess

ManageDataAccess筆以前的DataAccess更好用,不用考慮64位32位的問題

4.通過如下程式碼測試是否連線成功

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Oracle.ManagedDataAccess.Client;//dll引用
 
 
namespace test
{
    class Program
    {
        static void Main(string[] args)
        {
            string connString = "Data Source=orcl;User Id=zzw;Password=123456";
            OracleConnection conn = new OracleConnection();
            conn.ConnectionString = connString;
            conn.Open();
            Console.WriteLine("Connection State:" + conn.State);
            conn.Close();
            Console.ReadLine();
        }
    }
}

在我使用上述程式碼的時候出現"ORA-12154:tns:無法解析指定的連線識別符號",於是採用下述程式碼連線成功

OracleConnection conn = new OracleConnection();
           conn.ConnectionString = "Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=127.0.0.1)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=orcl)));Persist Security Info=True;User ID=lipzhang;Password=123456;";
           conn.Open(); 
           Console.WriteLine("Connection State:" + conn.State);
           conn.Close();
           Console.ReadLine();