廢話很少說直接正題:html
首先咱們先在Oracle數據庫下建了一個用戶叫作lisi,密碼爲lisi,在這個用戶下創建一張表叫作「USERS」,在這個表下新增三個數據。數據庫
方式一:利用OleDb鏈接Oracle。oracle
1 #region 使用OleDb方式 2 Console.WriteLine("OleDb方式"); 3 OleDbConnection oledbConnection = new OleDbConnection("Provider=OraOLEDB.Oracle; Data Source=MLDN; User Id=lisi; Password=lisi;"); 4 OleDbCommand oledbCommand = new OleDbCommand(); 5 oledbCommand.CommandText = "select * from USERS"; 6 oledbCommand.CommandType = CommandType.Text; 7 oledbCommand.Connection = oledbConnection; 8 oledbConnection.Open(); 9 var oledbReader = oledbCommand.ExecuteReader(); 10 while (oledbReader.Read()) 11 { 12 Console.WriteLine("姓名:"+oledbReader[1]); 13 } 14 oledbConnection.Close(); 15 #endregion
方式二:利用Oracle.ManagedDataAccess.Client方式,在使用前,咱們須要先在Nuget中找到Oracle.ManagedDataAccess,而後安裝,該方式好處之一是不須要在客戶端上安裝Oracle客戶端,該版本是由Oracle公司開發的,並且微軟已經放棄了本身的System.Data.OracleClient方式(下面會有介紹),推薦使用這種方式。ide
程式碼以下:工具
#region OracleConnection //OracleConnection connection = new OracleConnection("Password=lisi;User ID=lisi;Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.229.138)(PORT=1521)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=****)))");沒有安裝客戶端的話,可使用這種方式 Console.WriteLine("Oracle.ManagedDataAccess.Client方式"); OracleConnection oralceConnection = new OracleConnection("Data Source=MLDN; User Id=lisi; Password=lisi;"); oralceConnection.Open(); OracleCommand oracleCommand = new OracleCommand(); oracleCommand.CommandText = "select * from USERS"; oracleCommand.CommandType = CommandType.Text; oracleCommand.Connection = oralceConnection; var oracleReader = oracleCommand.ExecuteReader(); while (oracleReader.Read()) { Console.WriteLine("姓名:" + oracleReader[1]); } oralceConnection.Close(); #endregion
方式三:利用odbc方式spa
#region odbc Console.WriteLine("odbc方式"); OdbcConnection odbcConnection = new OdbcConnection("Driver={Microsoft ODBC for Oracle};Server=MLDN;Uid=lisi;Pwd=lisi;"); odbcConnection.Open(); OdbcCommand odbcCommand = new OdbcCommand(); odbcCommand.CommandText = "select * from USERS"; odbcCommand.CommandType = CommandType.Text; odbcCommand.Connection = odbcConnection; var odbcReader= odbcCommand.ExecuteReader(); while (odbcReader.Read()) { Console.WriteLine("姓名:" + odbcReader[1]); } odbcConnection.Close(); #endregion
方式四:利用System.Data.OracleClient方式(該方式已過期,不推薦使用)。.net
雖然已通過時了可是仍是可使用的,代碼以下:插件
1 #region 2 3 Console.WriteLine("System.Data.OracleClient方式"); 4 System.Data.OracleClient.OracleConnection clientConnection = new System.Data.OracleClient.OracleConnection(); 5 clientConnection.ConnectionString = " Data Source=MLDN; User Id=lisi; Password=lisi;"; 6 System.Data.OracleClient.OracleCommand clientCommand = new System.Data.OracleClient.OracleCommand(); 7 8 clientCommand.CommandText = "select * from USERS"; 9 clientCommand.Connection = clientConnection; 10 clientCommand.CommandType = CommandType.Text; 11 clientConnection.Open(); 12 var clientReader = clientCommand.ExecuteReader(); 13 while (clientReader.Read()) 14 { 15 16 Console.WriteLine("姓名:" + clientReader[1]); 17 } 18 clientConnection.Close(); 19 #endregion
方式五:使用能夠鏈接Oracle的Entity Framework工具,在使用前須要先安裝一個插件包,個人VS是2013的,插件包在以下地址:code
http://www.oracle.com/technetwork/cn/topics/dotnet/downloads/odacmsidownloadvs2013-2756823-zhs.htmlhtm
當安裝好了以後,咱們可使用EF去鏈接Oralce資料庫了,鏈接方式以下:
而後咱們在UI畫面中拉取咱們須要用到的表,注意使用EF的時候是要設置表主鍵的,因此咱們須要設置USERS這張表的ID爲主鍵,不然沒有辦法拉取到該表對象。
代碼以下:
#region Oracle EF Console.WriteLine("Oracle EF方式"); Entities db = new Entities(); var users = db.USERS.ToList(); foreach (var item in users) { Console.WriteLine("姓名:" + item.NAME); } #endregion
執行結果畫面:
綜上是我目前所知道的鏈接Oracle的方式,至於還有其餘方式,我暫時可能就不太清楚了。
參考文章:
http://blog.csdn.net/zz155666/article/details/53163115
https://www.cnblogs.com/gguozhenqian/p/4262813.html