dotConnect for Oracle入門指南(八):經過OracleCommand類使用存儲過程

【下載dotConnect for Oracle最新版本】數據庫

dotConnect for Oracle(原名OraDirect.NET)創建在ADO.NET技術上,爲基於Oracle數據庫的應用程序提供完整的解決方案。它爲設計應用程序結構帶來了新的方法,提升工做效率,使數據庫應用程序的開發更簡便。函數

本篇文章介紹如何在OracleCommand類的幫助下,使用Dotconnect for Oracle建立和使用Oracle存儲過程和函數。性能

有兩種經過OracleCommand執行存儲過程的通常方法。優化

第一種方法是將過程調用包含到PL/SQL塊中,並經過將其放入OracleCommand.CommandText屬性來執行該塊。在這種狀況下,該過程返回的數據能夠在同一塊中當即處理。若是過程須要一些參數,則應將它們添加到OracleCommand.Parameters集合中。此方法與一般的命令執行沒有區別。this

第二種方法是將OracleCommand.CommandType設置爲System.Data.commandType.StoredProcedure。在這種狀況下,CommandText應該設置爲過程的名稱。如下示例顯示如何使用上一節中的get-all-depts-proc過程填充數據表:spa

1scala

2設計

3code

4ci

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

// 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();

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

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將禁用檢查其餘光標參數。

相關文章
相關標籤/搜索