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将禁用检查其余光标参数。

相关文章
相关标签/搜索