今天来看看如何在dotConnect for Oracle中创建和使用Oracle存储过程,在OracleCommand类的帮助下用于Oracle的连接的函数。
通过oraclecommand执行存储过程一般有两种方式。第一种方法就是包括一个调用到一个PL/SQL块中,将这个块放到OracleCommand.CommandText属性中,就可以执行了。在这种情况下,由存储过程返回的数据可能在同一个块中一次处理。如果进程需要一些参数,参数可以被添加到OracleCommand.Parameters集。
第二种方法是将 OracleCommand.CommandType 设置为 System.Data.CommandType.StoredProcedure。在这种情况下,CommandText 应该设置为过程的名称。下面的示例演示如何使用get_all_depts_proc来填充一个数据表:
[C#]
// 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();
[Visual Basic]
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,使用存储功能而不是进程,相同的代码也将会填充数据表格。
标签:
本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,尊重他人劳动成果
文章转载自:慧都控件