dotConnect for Oracle入门指南(八):通过OracleCommand类使用存储过
【下載dotConnect for Oracle最新版本】
dotConnect for Oracle(原名OraDirect.NET)建立在ADO.NET技術上,為基于Oracle數(shù)據(jù)庫的應用程序提供完整的解決方案。它為設計應用程序結構帶來了新的方法,提高工作效率,使數(shù)據(jù)庫應用程序的開發(fā)更簡便。
本篇文章介紹如何在OracleCommand類的幫助下,使用Dotconnect for Oracle創(chuàng)建和使用Oracle存儲過程和函數(shù)。
有兩種通過OracleCommand執(zhí)行存儲過程的一般方法。
第一種方法是將過程調用包含到PL/SQL塊中,并通過將其放入OracleCommand.CommandText屬性來執(zhí)行該塊。在這種情況下,該過程返回的數(shù)據(jù)可以在同一塊中立即處理。如果過程需要一些參數(shù),則應將它們添加到OracleCommand.Parameters集合中。此方法與通常的命令執(zhí)行沒有區(qū)別。
第二種方法是將OracleCommand.CommandType設置為System.Data.commandType.StoredProcedure。在這種情況下,CommandText應該設置為過程的名稱。以下示例顯示如何使用上一節(jié)中的get-all-depts-proc過程填充數(shù)據(jù)表:
| 123456789101112131415161718192021222324 | // Open the connectionOracleConnection connection????=?new OracleConnection("Server=Ora; User Id=Scott; Password = tiger;");connection.Open();// Create a commandOracleCommand command =?new OracleCommand();command.Connection = connection;// Set the CommandType property to execute// stored procedures or functions by this commandcommand.CommandType = System.Data.CommandType.StoredProcedure;// Set the name of procedure or function to be executedcommand.CommandText =?"get_all_depts_proc";// The ParameterCheck property should be true to automatically// check the parameters needed for the procedure execution.command.ParameterCheck =?true;// At this moment, the command is ready for execution.// As we have an output cursor parameter, we may use the command to fill a data table.OracleDataTable dt =?new OracleDataTable(command, connection);dt.Fill(); |
| 12345678910111213141516171819202122 | Dim connection _????As New OracleConnection("Server=Ora; User Id=Scott; Password = tiger;")connection.Open()' Create a command.Dim command =?New OracleCommand()command.Connection = connection' Set the CommandType property to execute stored procedures or functions by this command.command.CommandType = System.Data.CommandType.StoredProcedure' Set the name of procedure or function to be executed.command.CommandText =?"get_all_depts_proc"' The ParameterCheck property should be true to automatically' check the parameters needed for the procedure execution.command.ParameterCheck =?True' At this moment, the command is ready for execution.' As we have an output cursor parameter, we may use the command to fill a data table.Dim dt =?New OracleDataTable(command, connection)dt.Fill() |
將CommandText設置為“get-all-depts-func”,相同的代碼使用存儲函數(shù)而不是過程填充數(shù)據(jù)表。
優(yōu)化存儲過程執(zhí)行
當執(zhí)行ExecuteReader或ExecuteEscalar時,并且OracleCommand.CommandType設置為System.Data.commandType.StoredProcedure時,默認情況下將執(zhí)行附加查詢,以檢查過程是否是流水線的,如果不是,則說明參數(shù)(簽出光標參數(shù))。這允許您在僅設置必要的過程參數(shù)后執(zhí)行存儲過程,而不必費心完全正確地填充參數(shù)集合,因為在獲取元數(shù)據(jù)后,它將自動填充。
但是,執(zhí)行附加查詢可能不合適,并且在某些情況下可能會導致性能損失。Dotconnect for Oracle允許使用DescribeStoredProcedure連接字符串參數(shù)禁用此檢查。
如果只將此連接字符串參數(shù)設置為false,OracleCommand將執(zhí)行存儲的例程,而不進行任何額外的檢查。在這種情況下,例程不能是表值函數(shù),它的所有參數(shù)都必須手動設置。
如果要在不進行其他檢查的情況下執(zhí)行表值函數(shù),則需要將OracleCommand的IsTableValuedFunction屬性設置為true。這允許您在不進行額外檢查的情況下執(zhí)行表值函數(shù)。將此屬性設置為true也是執(zhí)行非管道表值函數(shù)的唯一方法。即使describeStoredProcedure設置為true,也必須將IsTableValuedFunction設置為true才能執(zhí)行非管道表值函數(shù)。
如果只對OracleCommand的單個實例禁用附加檢查,而不禁用連接的附加檢查,請將IsTableValuedFunction屬性(根據(jù)執(zhí)行的函數(shù)是否為表值,設置為true或false)和ImplicitRefCursors屬性設置為false。設置IsTableValuedFunction屬性將禁用檢查執(zhí)行的函數(shù)是否為表值,并將ImplicitRefCursors屬性設置為false將禁用檢查其他光標參數(shù)。
轉載于:https://blog.51cto.com/14048826/2346066
總結
以上是生活随笔為你收集整理的dotConnect for Oracle入门指南(八):通过OracleCommand类使用存储过的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Linux学习8之Shell编程--基础
- 下一篇: 警惕!这7件事情千万不要发生你身上-来自