用.NET新提供的managedprovider來訪問Oracle資料(轉)
用.NET新提供的managedprovider來訪問Oracle資料(轉)[@more@] 微軟公司把SQL Server提升為.NET應用程式的資料庫,但是,由於Oracle很高的市場佔有率,你無疑需要在.NET應用程式中使用Oracle資料。在ADO.NET中,Oracle資料是利用通用的OLE-DB provider來訪問的,但是這在效能上落後於SQL Server的管理提供者(managed provider)。微軟公司認識到了有必要專門為Oracle提供管理者(provider),因此它釋出了 讓我們看看如何利用它來訪問資料的。 得到你所需要的 在以前,人們常常用“驅動(driver)”這一詞來表示讓應用程式訪問資料庫的工具。現在,在.NET下,人們用“管理提供者(managed provider)”來取代“驅動”一詞。.NET Managed Provider For Oracle是對.NET的一個附加,它依賴於Oracle客戶軟體來與Oracle資料庫介面。所以,除了擁有.NET提供者之外,你還需要安裝Oracle客戶軟體。 你還需要在Visual Studio .NET中新增對System.Data.OracleClient.dll的引用,為此,你可以在Visual Studio .NET中滑鼠右擊References列表或者從選單欄中選擇Project | Add Reference。你也許還需要在你的應用程式中正確匯入System.Data.OracleClient名字空間。 訪問資料 .NET環境的一大優點就是資料庫訪問程式碼通常遵循相同的模式而與後端資料庫無關。首先,你建立一個連線,然後配置SQL語句或者指令。最後,執行SQL(語句或者命令)並處理結果。 顯而易見,用來訪問Oracle資料庫的程式碼與用來訪問SQL Server的程式碼不會有很大的區別。這兩者的物件名字不同,但是過程是相似的,你可以在清單A中的VB.NET程式碼中很清楚的看出這一點。 清單A如下: Listing A: Accessing Oracle data Dim oracleConnection As New OracleConnection() Dim oracleCommand As New OracleCommand() Dim oracleReader As OracleDataReader Dim sqlString As String sqlString = "data source=oracle.server;user id=test;password=test" oracleConnection.ConnectionString = oracleConnection.Open() oracleCommand.CommandText = "SELECT * FROM DBTEST.PEOPLE" oracleCommand.Connection = oracleConnection oracleReader = oracleCommand.ExecuteReader 清單A中的程式碼建立一個連線、建立一個命令物件(該物件用來返回PEOPLE表中所有的行),並用OracleDataReader(它與SqlDataReader和OleDbDataReader物件相似)來讀取資料。 如果你用其它.NET環境來處理SQL Server,你會立即注意到你所用到的類的名字與Oracle對應類的名字是相似的。例如,SQL Server連線類命名為SqlConnection,而Oracle連線類命名為OracleConnection。 提防錯誤 在所有的.NET開發中,你都應該密切監視例外的產生以確保在訪問資料時程式碼執行無誤。OracleClient名字空間包括了一個特定於Oracle的例外類,即OracleException,它可以用在標準的try…catch程式碼塊中。清單B給出的程式碼就利用上述方法來捕獲任何例外。清單B給出了一個如何在執行時處理例外的例子。 清單B如下: Listing B: Exception handling Dim oracleConnection As New OracleConnection() Dim oracleCommand As New OracleCommand() Dim oracleReader As OracleDataReader Dim sqlString As String sqlString = "data source=oracle.server;user id=test;password=test" Try oracleConnection.ConnectionString = sqlString oracleConnection.Open() oracleCommand.CommandText = "SELECT * FROM DBTEST.PEOPLE" oracleCommand.Connection = oracleConnection oracleReader = oracleCommand.ExecuteReader ' Work with oracle data oracleReader.Close() oracleCommand.Dispose() Catch ex As OracleException 'handle the exception Catch ex As Exception 'handle the exception Finally If Not (oracleConnection Is Nothing) Then oracleConnection.Close() End If End Try 儘管在本文中我用到了OracleDataReader類,但實際上Oracle資料提供者包括了所有標準ADO.NET類(如OracleDataSet和OracleDataAdapter)的各種版本。微軟公司推出的.NET Managed Provider For Oracle提供了自由訪問Oracle資料的必備工具,其效能遠優於對OLE-DB的訪問。ADO.NET類的設計對資料庫的訪問所用的方法相同、物件相似,與資料庫型別無關。因此,OLE-DB專案可以很容易轉換到Oracle下。
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/8225414/viewspace-957115/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 用VC訪問Sybase資料庫(用Sybase提供的ct-library介面) (轉)資料庫
- 用perl訪問mysql資料庫(轉)MySql資料庫
- 如何限定IP訪問Oracle資料庫-轉Oracle資料庫
- 用JDBC訪問一個資料庫(轉)JDBC資料庫
- DotNetNuke 資料訪問層
- 使用Oracle Net實現限制特定IP訪問資料庫Oracle資料庫
- 用伺服器提供的資料來源,hibernate出現問題。伺服器
- ASP.NET 2.0 中的資料訪問ASP.NET
- Oracle資料庫訪問控制Oracle資料庫
- 遠端資料庫的訪問 (轉)資料庫
- 用連線池提高Servlet訪問資料庫的效率 (轉)Servlet資料庫
- Oracle,SqlServer,Access資料庫通用訪問類設計(轉)OracleSQLServer資料庫
- Oracle資料庫限制訪問IPOracle資料庫
- .net之oraclecommand訪問oracleOracle
- [開源] .Net ORM 訪問 Firebird 資料庫ORM資料庫
- Oracle資料訪問元件ODAC的安裝方法Oracle元件
- 如何限制ip訪問Oracle資料庫Oracle資料庫
- Oracle資料庫訪問效能優化Oracle資料庫優化
- 透過socket訪問資料庫(轉)資料庫
- JSP訪問資料庫大全(轉)JS資料庫
- MySql資料庫C++訪問(轉)MySql資料庫C++
- SpringBoot資料訪問之Druid資料來源的使用Spring BootUI
- 用Python訪問OraclePythonOracle
- 用JDBC訪問一個資料庫JDBC資料庫
- Nginx提供perl CGI訪問Nginx
- Sql Server在.net中的資料訪問輔助類SQLServer
- 釋出一個.NET資料庫訪問類資料庫
- 【方法】如何限定IP訪問Oracle資料庫Oracle資料庫
- 禁止某個IP訪問Oracle資料庫Oracle資料庫
- JDBC用ResultSet訪問大量資料時會遇到的問題JDBC
- Applet直接訪問資料庫 (轉)APP資料庫
- 寫一個通用資料訪問元件 (轉)元件
- Oracle資料鏈+同義詞+訪問遠端資料庫Oracle資料庫
- [開源] .Net 使用 ORM 訪問 華為GaussDB資料庫ORM資料庫
- 【oracle】sqlnet.ora 訪問控制策略OracleSQL
- grpc提供http訪問方式RPCHTTP
- Python訪問Oracle的兩種資料獲取方式PythonOracle
- jboss訪問資料庫的問題資料庫