Working With Stored Procedures Using dotConnect for Oracle

Introduction

A stored procedure comprises a set of SQL statements and PL/SQL constructs that are grouped together. Stored procedures, as well as functions, can have input, output as well as input/output parameters. dotConnect for Oracle provides excellent support for working with stored procedures.

It is a fast, scalable ORM for Oracle from Devart that is built on top of ADO.NET and provides you an opportunity to connect to and work with Oracle databases from your .NET or .NET Core applications. You can use this framework in your WinForms, WPF, ASP.NET, and ASP.NET Core applications.

The article talks briefly about the striking features of dotConnect for Oracle and shows how to work with stored procedures and dotConnect for Oracle in ASP.NET Core 5.

Prerequisites

To be able to work with the code examples demonstrated in this article, you should have the following installed in your system:

You can download .NET 5.0 runtime from here.

You can download Visual Studio 2019 from here.

You can download Oracle Express Edition from here.

You can download a copy of dotConnect for Oracle from here.

Create a New ASP.NET Core 5.0 Project in Visual Studio 2019

Assuming that the necessary software has been installed in your computer to be able to work with Entity Developer, follow the steps outlined below to create a new ASP.NET Core Web API project.

Create a new project with ASP.NET Core Web API highlighted

Select .NET 5.0 as target framework

You should disable the "Configure for HTTPS" and "Enable Docker Support" options by disabling the respective checkboxes. Since we'll not be using authentication in this example, specify authentication as "No Authentication." Finally, click on the "Create" button to finish the process.

dotConnect For Oracle: Features and Benefits

Microsoft’s ADO.NET is a managed framework that can be used to connect to and work with several databases. dotConnect for Oracle is a high-performance ORM that sits on top of the ADO.NET data provider and enables you to perform CRUD operations against Oracle databases. It is a managed framework that is high performant and is easy to deploy supports load balancing, batch processing, asynchronous command execution, and data binding. It also supports several Oracle features that include Advanced Queuing, Change Notifications, Alerts, Pipes, Direct Path Loading, etc.

Create a Database Table

The following is the syntax for creating a new table in Oracle. Note how schema name and constraints are specified.

SQL
 




xxxxxxxxxx
1


 
1
CREATE TABLE schema_name.table_name (
2
    column_1 data_type column_constraint,
3
    column_2 data_type column_constraint,
4
    ...
5
    table_constraint
6
 );



The following code snippet can be used to create a new table called product in Oracle.

SQL
 




xxxxxxxxxx
1


 
1
CREATE TABLE product
2
  (
3
    product_id NUMBER PRIMARY KEY,
4
    product_name VARCHAR2(50) NOT NULL,
5
    quantity NUMBER NOT NULL
6
  );


Creating the Stored Procedure

To create a stored procedure in Oracle, use the following piece of code:

SQL
 




xxxxxxxxxx
1


 
1
CREATE PROCEDURE product_insert(pid INTEGER, pname VARCHAR, quantity INTEGER) AS
2
BEGIN
3
  INSERT INTO product(product_id, product_name, quantity) VALUES (pid, pname, qty);
4
END;


Creating OracleConnection

To connect to the Oracle database, you should provide the database credentials. This information is stored inside a connection string. The connection string comprises the server name, user Id, password, etc.

You can create OracleConnection in two different ways, i.e., design time and run time. You can create an OracleConnection at design time from the Toolbox inside the Visual Studio IDE. To create an instance of OracleConnection at run-time, you can use the following code snippet:

C#
 




xxxxxxxxxx
1


 
1
OracleConnection oracleConnection = new OracleConnection();
2
oracleConnection.Server = "DemoXServer";
3
oracleConnection.UserId = "scott";
4
oracleConnection.Password = "tiger";



You should include the following namespace in your program:

C#
 




xxxxxxxxxx
1


 
1
using Devart.Data.Oracle;



Executing a Stored Procedure Using the OracleCommand Class

You can execute an OracleProcedure in the following ways:

The following source code illustrates how you can execute a stored procedure to read data from the database table we've created earlier.

C#
 




xxxxxxxxxx
1


 
1
OracleConnection connection = new OracleConnection("Server=DemoXServer; User Id=scott; Password = tiger;");
2
connection.Open();
3
OracleCommand command = new OracleCommand();
4
command.Connection = connection;
5
command.CommandType = System.Data.CommandType.StoredProcedure;
6
command.CommandText = "getallproducts";
7
command.ParameterCheck = true;
8
OracleDataTable dataTable = new OracleDataTable(command, connection);
9
dataTable.Fill();



Working With Oracle Stored Procedures and DataSets

You can work with Oracle Stored Procedures with DataSets in two ways: using the DataSet Wizard as well as programmatically. If your stored procedure returns a result set, you can use this result set to generate a table in a typed data set.

Assuming you've created a DataSet already, you can use the following code to bind data to your data controller.

C#
 




xxxxxxxxxx
1


 
1
dataSet.Fill();
2
DataTable dataTable = dataSet.Tables[0];
3
dataGridView.DataSource = dataTable;



You can use the DataSet Wizard to update, insert, or delete data.

Working With Table-Valued Functions and dotConnect for Oracle

Table-valued functions in Oracle are functions return collections of rows. These functions return collections and can be queried similar to database tables. The following are the advantages of Table-value functions:

The following code snippet illustrates how you can work with table-valued functions in Oracle using dotConnect for Oracle to retrieve the top 5 records from the product table in the database.

C#
 




x
22


 
1
using (OracleConnection connection = new OracleConnection("Data Source=DemoXServer; User Id=scott; Password=tiger; DescribeStoredProcedure=false;")) 
2
 
          
3
 {
4
 
          
5
    connection.Open();
6
    using (OracleCommand command = 
7
    connection.CreateCommand("get_product_function", 
8
    CommandType.StoredProcedure)) 
9
    {
10
 
          
11
       command.IsTableValuedFunction = true;
12
       command.Parameters.Add("row_ctr", 5);
13
       List<object[]> data = new List<object[]>();
14
 
          
15
       using (OracleDataReader reader = cmd.ExecuteReader()) 
16
       {
17
 
          
18
          while (reader.Read())
19
          data.Add(reader.GetValues());
20
       }
21
    }
22
 }



Summary

Stored procedures are PL/SQL blocks that perform one or more specific tasks that enable you to reuse a set of PL/SQL statements if and when needed. You can know more on dotConnect for Oracle from their online documentation.

 

 

 

 

Top