個人操做系統是win10 x64 ,服務器oracle版本是g11html
服務器上oracle作一些配置sql
而後更改tnsnames.ora數據庫
添加你的配置服務器
個人例如:oracle
ddd=zkrOracle108 = (DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.51.108)(PORT = 1521)) ) (CONNECT_DATA = (SERVER = DEDICATED) (SID = DB11G) ) )
配置環境變量spa
NLS_LANG=SIMPLIFIED CHINESE_CHINA.ZHS16GBK 操作系統
採用OracleClient進行鏈接數據庫debug
在oracle官網上下載Instant Client 程序包3d
地址:http://www.oracle.com/technetwork/cn/topics/winx64soft-101515-zhs.htmlcode
而後放到最後生成的dll文件夾中,例如debug
而後程序代碼
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.OracleClient; using System.Configuration; using System.Data; namespace ConsoleDBTool { class Program { static void Main(string[] args) { string connstr = ConfigurationManager.ConnectionStrings["ORACLEConnectionString"].ConnectionString; //todo:SERVER寫成SERVER_NAME會報出 ORA-12514: TNS: 監聽程序當前沒法識別鏈接描述符中請求的服務 //password錯誤會提示:ORA-01017: 用戶名/口令無效; 登陸被拒絕 //沒有寫(SID = DB11G)會提示:ORA-12504: TNS: 監聽程序在 CONNECT_DATA 中未得到 SERVICE_NAME //沒寫(SERVER=DEDICATED)會提示:媽的!能夠成功返回 //沒寫DESCRIPTION =(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.51.108)(PORT = 1521))) ORA-12533: TNS: 非法的 ADDRESS 參數 //HOST地址配錯通常會提示鏈接超時 //從網上覆制的這種 string sqlcon = @"Data Source=TestDB;Persist Security Info=True;User ID=tanyixiu;Password=pwd;Unicode=True";,他媽的絕對是不行的! //只要提示是DRA開頭,說明你已經和oracle發生「握手」了,若是出現System.Data.OracleClient 須要 Oracle 客戶端軟件 version 8.1.7 或更高版本說明沒有完成Instant Client的安裝或環境變量的配置 //從昨天下午開始搞這個,到16:00才連上oracle,我以爲是上帝玩夠了,不想和你開玩笑了,因此你就成功了。 //var connectionString = @"Data Source=(DESCRIPTION =(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.51.108)(PORT = 1521)))(CONNECT_DATA =(SERVER = DEDICATED)(SID = DB11G)));User Id=123;Password=123;Persist Security Info=True;";// OracleConnection oc = new OracleConnection(connstr); oc.Open(); OracleDataAdapter od = new OracleDataAdapter("select * from dir_test", connstr); DataSet ds = new DataSet(); od.Fill(ds); Console.WriteLine("數據庫鏈接成功"); } } }
config:
<?xml version="1.0"?> <configuration> <startup> <supportedRuntime version="v2.0.50727"/> </startup> <connectionStrings> <add name="ORACLEConnectionString" connectionString="Data Source=(DESCRIPTION =(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.51.108)(PORT = 1521)))(CONNECT_DATA =(SERVER = DEDICATED)(SID = DB11G)));User Id=123;Password=123;Persist Security Info=True;"/> </connectionStrings> </configuration>