DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • Building a RESTful Service Using ASP.NET Core and dotConnect for PostgreSQL
  • Working With Stored Procedures Using dotConnect for Oracle
  • Implementing Cache Dependency in ASP.NET Core
  • Working With dotConnect for SQL Server in ASP.NET Core

Trending

  • IoT and Cybersecurity: Addressing Data Privacy and Security Challenges
  • How GitHub Copilot Helps You Write More Secure Code
  • Prioritizing Cloud Security Risks: A Developer's Guide to Tackling Security Debt
  • How Kubernetes Cluster Sizing Affects Performance and Cost Efficiency in Cloud Deployments
  1. DZone
  2. Data Engineering
  3. Data
  4. Working With dotConnect for Oracle in ASP.NET Core

Working With dotConnect for Oracle in ASP.NET Core

The article discusses the striking features of dotConnect for Oracle (a fast ORM for Oracle from Devart) and shows how to work with it in ASP.NET Core.

By 
Joydip Kanjilal user avatar
Joydip Kanjilal
DZone Core CORE ·
Apr. 15, 21 · Analysis
Likes (2)
Comment
Save
Tweet
Share
10.0K Views

Join the DZone community and get the full member experience.

Join For Free

Introduction

dotConnect for Oracle is a fast 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. It is a fast, scalable data access framework that can be used in WinForms, ASP.NET, etc.

The article discusses the striking features of dotConnect for Oracle and shows how to work with it in ASP.NET Core.

Prerequisites

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

  • Visual Studio 2019 Community Edition
  • .NET 5
  • Oracle database
  • dotConnect for Oracle

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.

  • First off, open the Visual Studio 2019 IDE.
  • Next, click "Create a new project" once the IDE has loaded.
  • In the "Create a new project" screen, select “ASP.NET Core Web API” as the project template.

Create a new project and select "ASP.NET Core Web API"

  • Click the "Next" button
  • Specify the project name and location where it should be stored in your system
  • Optionally, click the "Place solution and project in the same directory" checkbox.
  • Next, click the "Create" button
  • In the "Create a new ASP.NET Core Web Application" dialog window that is shown next, select "API" as the project template.
  • In the “Additional Information” screen, select .NET 5.0 as the framework version.

Set .NET 5.0 as the framework version in "Additional Information"

  • 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. You can take advantage of ADO.NET to connect to any database for which a database driver is available. As long as ADO.NET can communicate with that database driver, you’re good to go!

dotConnect for Oracle is a high-performance ORM enables data provider that works on top of ADO.NET to provide you a complete solution for performing CRUD operations against Oracle databases. It supports several Oracle features that include Advanced Queuing, Change Notifications, Alerts, Pipes, Direct Path Loading, etc.

Some of the key features of dotConnect for Oracle include the following:

  • High performance
  • Easy deployment
  • Support for Entity Framework
  • 100% managed code
  • Easy to deploy
  • Easy to update to a new version via NuGet packages
  • Support for load balancing and batch processing
  • Support for asynchronous command execution
  • Excellent data binding capabilities

Create a Database Table

The following is the syntax for creating a new table in Oracle. Note how the 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 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;



Reading Data in Connected Mode

The following code snippet illustrates how you can read data using dotConnect for Oracle.

C#
 




xxxxxxxxxx
1
22


 
1
String connectionString = “User Id=scott;Password=tiger;Server=DemoXServer;”;
2

          
3
using (OracleConnection oracleConnection = new OracleConnection (connectionString))
4
{
5

          
6
 OracleCommand oracleCommand = new OracleCommand();
7
 oracleCommand.CommandText = "SELECT * FROM product";
8
 oracleCommand.Connection = oracleConnection;
9

          
10
 using (OracleDataReader oracleDataReader = oracleCommand.ExecuteReader())
11
 {
12
   for (int i = 0; i < oracleDataReader.FieldCount; i++)
13
       Console.Write(oracleDataReader.GetName(i).ToString() + "\t");
14
   Console.WriteLine("");
15
   while (reader.Read())
16
   {
17
       for (int i = 0; i < oracleDataReader.FieldCount; i++)
18
          Console.Write(oracleDataReader.GetValue(i).ToString() + "\t");
19
       Console.WriteLine();
20
   }
21
 }
22
}



Note that you should close the data reader once you're done using it. To do this, you can call the Close method on the data reader instance or put the data reader inside a using block.

Retrieving Data in the Disconnected Mode

To retrieve data in a disconnected model in ADO.NET, you'd typically be using Data Adapter, DataSet, and DataTable. Devart OracleDataTable and OracleDataSet are adept at working in a disconnected mode; they have advanced features that would help you to work in the disconnected mode seamlessly. The following code snippet illustrates how you can work with OracleDataTable in the disconnected mode.

C#
 




xxxxxxxxxx
1
20


 
1
String connectionString = “User Id=scott;Password=tiger;Server=DemoXServer;”;
2
OracleDataTable dataTable = new OracleDataTable("SELECT * FROM product", connectionString);
3
   try
4
   {
5
       dataTable.FetchAll = true; 
6
       dataTable.Active = true;
7
       foreach (DataRow dataRow in dataTable.Rows)
8
       {
9
          foreach (DataColumn dataColumn in dataTable.Columns)
10
          {
11
              Console.Write(dataRow[dataColumn] + "\t");
12
          }
13

          
14
          Console.WriteLine();
15
       }
16
   }
17
   finally
18
   {
19
      dataTable.Active = false;
20
   }



Refer to the preceding code snippet. The FetchAll property when set to true implies that the data would be retrieved entirely from the server. Note the value of the FetchAll property is false by default which in turn implies that only minimal records will be requested at a time.

Updating Data

You can update data in two different ways: by updating the record directly in the connected mode, or, read the data to be updated, update it in memory, and then take advantage of the UPDATE statement to update the record(s).

To update data in your oracle database, you can take advantage of the UPDATE statement as shown in the following code snippet:

C#
 




xxxxxxxxxx
1
19


 
1
String connectionString = “User Id=scott;Password=tiger;Server=DemoXServer;”;
2
OracleConnection oracleConnection = new OracleConnection (connectionString);
3
OracleCommand oracleCommand = new OracleCommand();
4
oracleCommand.CommandText = “UPDATE product SET product_name = ‘Dell Laptop’ WHERE product_id = 1”;
5
oracleCommand.Connection = oracleConnection;
6
try 
7
{
8
  oracleConnection.Open();
9
  int x = oracleCommand.ExecuteNonQuery();
10
  Console.WriteLine(x + “ rows were affected.”);
11
}
12
catch 
13
{
14
  Console.WriteLine("Error encountered...");
15
}
16
finally 
17
{
18
  oracleConnection.Close();
19
}



The preceding code snippet can be used to update data in the product table using dotConnect for Oracle.

Inserting Data Into the Database

You can insert data both in the connected mode as well as the disconnected mode. In the connected mode, you can insert your data directly when the connection is open. In the disconnected mode, you can store the record(s) to be inserted in a Dataset and then use a DataAdapter to update the record(s) at one go.

You can write the following code to insert data to the product table using dotConnect for Oracle.

C#
 




x
21


 
1
String connectionString = “User Id=scott;Password=tiger;Server=DemoXServer;”;
2
OracleConnection oracleConnection = new OracleConnection (connectionString);
3
OracleCommand oracleCommand = new OracleCommand();
4
oracleCommand.CommandText = "INSERT INTO product (product_id, product_name, quantity) VALUES (1,'Lenovo Laptop',25)";
5
oracleCommand.Connection = oracleConnection;
6
try 
7
{
8
  oracleConnection.Open();
9
  int x = oracleCommand.ExecuteNonQuery();
10
  Console.WriteLine(x + " rows were affected.");
11
}
12

          
13
catch 
14
{
15
  Console.WriteLine("Error encountered...");
16
}
17

          
18
finally 
19
{
20
  oracleConnection.Close();
21
}



Summary

dotConnect for Oracle is a high-performance data provider for Oracle and is available in three editions, i.e., Professional, Mobile, and Express. You can know more on dotConnect for Oracle from their online documentation.

Database ASP.NET ASP.NET Core Data binding .NET

Opinions expressed by DZone contributors are their own.

Related

  • Building a RESTful Service Using ASP.NET Core and dotConnect for PostgreSQL
  • Working With Stored Procedures Using dotConnect for Oracle
  • Implementing Cache Dependency in ASP.NET Core
  • Working With dotConnect for SQL Server in ASP.NET Core

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!