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

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Fixing Common Oracle Database Problems
  • How To Replicate Oracle Data to BigQuery With Google Cloud Datastream
  • SAP HANA Triggers: Enhancing Database Logic and Automation
  • The Future of Data Lakehouses: Apache Iceberg Explained

Trending

  • Java’s Next Act: Native Speed for a Cloud-Native World
  • Google Cloud Document AI Basics
  • Unlocking AI Coding Assistants Part 4: Generate Spring Boot Application
  • Breaking Bottlenecks: Applying the Theory of Constraints to Software Development
  1. DZone
  2. Data Engineering
  3. Databases
  4. Bulk Data Insertion into Oracle Database in C#

Bulk Data Insertion into Oracle Database in C#

Bulk insertion of data into database table is a big overhead in application development.

By 
Ayobami Adewole user avatar
Ayobami Adewole
·
Jan. 28, 15 · Tutorial
Likes (0)
Comment
Save
Tweet
Share
44.1K Views

Join the DZone community and get the full member experience.

Join For Free

Introduction 

Bulk insertion of data into database table is a big overhead in application development. An approach would be to insert the records into the table using a loop in the application, this round trip approach consumes network and CPU resources and as such is not recommended. Microsoft ADO.Net provides the SqlBulkCopy class that lets you efficiently bulk load a SQL Server table with data from another source, but bulk loading an Oracle table can be done through the use of Oracle Data Provider for .Net (ODP.Net). 

Bulk Insertion with ODP.Net 

ODP.Net has an array binding feature in which the records to be inserted are put in arrays and the arrays are passed to the ODP.Net OracleCommand class for insertion into the database table.This blog post would use a simple application that insert multiple customer records into a Customer table in an Oracle database. The source code is below. 

// A customer DTO 
public class CustomerDTO
 { 
public string Surname{get;set;}
public string FirstName{get;set;}
public string EmailAddress{get;set;} 
 }

The OracleDbConnector class that serves as wrapper around ODP.net to perform bulk insertion of data. 

public class OracleDbConnector
 {
private OracleConnection oracleConnection;
private OracleConnectionStringBuilder stringBuilder;

public OracleDbConnector(string dbUsername, string dbPassword, string dbServer)
{
stringBuilder = new OracleConnectionStringBuilder();
stringBuilder.DataSource = dbServer;
stringBuilder.UserID = dbUsername;
stringBuilder.Password = dbPassword;
oracleConnection = new OracleConnection();
oracleConnection.ConnectionString = stringBuilder.ConnectionString;
}

public bool UploadBulkData(List<CustomerDTO> bulkData)
{
bool returnValue = false;
try
{

string query = @"insert into PCMS.Customer ( surname, firstName, emailAddress) values 
(:surname, :firstName, :emailAddress)";
oracleConnection.Open();
using (var command = oracleConnection.CreateCommand())
{
command.CommandText = query;
command.CommandType = CommandType.Text;
command.BindByName = true;
  // In order to use ArrayBinding, the ArrayBindCount property
  // of OracleCommand object must be set to the number of records to be inserted
command.ArrayBindCount = bulkData.Count;
command.Parameters.Add(":surname", OracleDbType.Varchar2, bulkData.Select(c => c.Surname).ToArray(), ParameterDirection.Input);
command.Parameters.Add(":firstName", OracleDbType.Varchar2, bulkData.Select(c => c.FirstName).ToArray(), ParameterDirection.Input);
command.Parameters.Add(":emailAddress", OracleDbType.Varchar2, bulkData.Select(c => c.EmailAddress).ToArray(), ParameterDirection.Input);
int result = command.ExecuteNonQuery();
if (result == bulkData.Count)
returnValue = true;
}

}
catch (OracleException ex)
{
//Log error thrown
}
finally
{
oracleConnection.Close();
}
return returnValue;
}
 }

An NUnit test case for the OracleDbConnector class. 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NUnit.Framework;

[TestFixture]
public class OracleDbConnectorTest
{
private OracleConnector oracleConnector;
[TestFixtureSetUp]
public void SetUpTest()
{
oracleConnector = new OracleConnector("UserId", "Password", "serverAddress");
}

[Test]
public void TestUploadBulkData()
{
List<CustomerDTO> bulkData= new List<CustomerDTO>();
bulkData.Add(new CustomerDTO("Ayobami","Adewole","xyz@abc.com"));
bulkData.Add(new CustomerDTO("John","Peter","john@abc.com"));
bulkData.Add(new CustomerDTO("Tunji","James","james@abc.com"));
Assert.IsTrue(oracleConnector.UploadBulkData(bulkData));
}

[TestFixtureTearDown]
public void TearDownTest()
{
oracleConnector = null;
}
}
}
Database Data (computing) Oracle Database

Published at DZone with permission of Ayobami Adewole, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Fixing Common Oracle Database Problems
  • How To Replicate Oracle Data to BigQuery With Google Cloud Datastream
  • SAP HANA Triggers: Enhancing Database Logic and Automation
  • The Future of Data Lakehouses: Apache Iceberg Explained

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!