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集合中。此方法与通常的命令执行没有区别。

第二种方法是将OracleCommand.CommandType设置为System.Data.commandType.StoredProcedure。在这种情况下,CommandText应该设置为过程的名称。以下示例显示如何使用上一节中的get-all-depts-proc过程填充数据表:

1

2

3

4

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

猜你喜欢

转载自blog.csdn.net/weixin_43577863/article/details/86621196