翻譯|使用教程|編輯:李爽夏|2019-01-24 10:02:04.000|閱讀 304 次
概述:本篇文章介紹如何在OracleCommand類的幫助下,使用Dotconnect for Oracle創建和使用Oracle存儲過程和函數。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
【下載dotConnect for Oracle最新版本】
dotConnect for Oracle(原名OraDirect.NET)建立在ADO.NET技術上,為基于Oracle數據庫的應用程序提供完整的解決方案。它為設計應用程序結構帶來了新的方法,提高工作效率,使數據庫應用程序的開發更簡便。
本篇文章介紹如何在OracleCommand類的幫助下,使用Dotconnect for Oracle創建和使用Oracle存儲過程和函數。
有兩種通過OracleCommand執行存儲過程的一般方法。
第一種方法是將過程調用包含到PL/SQL塊中,并通過將其放入OracleCommand.CommandText屬性來執行該塊。在這種情況下,該過程返回的數據可以在同一塊中立即處理。如果過程需要一些參數,則應將它們添加到OracleCommand.Parameters集合中。此方法與通常的命令執行沒有區別。
第二種方法是將OracleCommand.CommandType設置為System.Data.commandType.StoredProcedure。在這種情況下,CommandText應該設置為過程的名稱。以下示例顯示如何使用上一節中的get-all-depts-proc過程填充數據表:
// Open the connection OracleConnection connection = new OracleConnection("Server=Ora; User Id=Scott; Password = tiger;"); connection.Open(); // Create a command OracleCommand 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. OracleDataTable dt = new OracleDataTable(command, connection); dt.Fill();
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”,相同的代碼使用存儲函數而不是過程填充數據表。
當執行ExecuteReader或ExecuteEscalar時,并且OracleCommand.CommandType設置為System.Data.commandType.StoredProcedure時,默認情況下將執行附加查詢,以檢查過程是否是流水線的,如果不是,則說明參數(簽出光標參數)。這允許您在僅設置必要的過程參數后執行存儲過程,而不必費心完全正確地填充參數集合,因為在獲取元數據后,它將自動填充。
但是,執行附加查詢可能不合適,并且在某些情況下可能會導致性能損失。Dotconnect for Oracle允許使用DescribeStoredProcedure連接字符串參數禁用此檢查。
如果只將此連接字符串參數設置為false,OracleCommand將執行存儲的例程,而不進行任何額外的檢查。在這種情況下,例程不能是表值函數,它的所有參數都必須手動設置。
如果要在不進行其他檢查的情況下執行表值函數,則需要將OracleCommand的IsTableValuedFunction屬性設置為true。這允許您在不進行額外檢查的情況下執行表值函數。將此屬性設置為true也是執行非管道表值函數的唯一方法。即使describeStoredProcedure設置為true,也必須將IsTableValuedFunction設置為true才能執行非管道表值函數。
如果只對OracleCommand的單個實例禁用附加檢查,而不禁用連接的附加檢查,請將IsTableValuedFunction屬性(根據執行的函數是否為表值,設置為true或false)和ImplicitRefCursors屬性設置為false。設置IsTableValuedFunction屬性將禁用檢查執行的函數是否為表值,并將ImplicitRefCursors屬性設置為false將禁用檢查其他光標參數。
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn