Over a million developers have joined DZone.

Bulk Data Insertion into Oracle Database in C#

DZone's Guide to

Bulk Data Insertion into Oracle Database in C#

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

· Database Zone ·
Free Resource

Download the Altoros NoSQL Performance Benchmark 2018. Compare top NoSQL solutions – Couchbase Server v5.5, MongoDB v3.6, and DataStax Enterprise v6 (Cassandra).


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;

string query = @"insert into PCMS.Customer ( surname, firstName, emailAddress) values 
(:surname, :firstName, :emailAddress)";
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
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;

public class OracleDbConnectorTest
private OracleConnector oracleConnector;
public void SetUpTest()
oracleConnector = new OracleConnector("UserId", "Password", "serverAddress");

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"));

public void TearDownTest()
oracleConnector = null;

Download the whitepaper, Moving From Relational to NoSQL: How to Get Started. We’ll take you step by step through your first NoSQL project.

sql ,dotnet ,how-to ,microsoft ,visual studio ,data ,data access ,.net & windows

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}