Two Ways To Insert Bulk Data Into Oracle Database Using C#

This article will explain how we can insert bulk amount of data in Oracle database using C#. Generally, what happens, we insert one by one record into the database, but sometimes, we have requirements to insert numerous amount of data into the database using one go. Means, rather than going one by one process which is a lengthy and time-consuming process, we prefer to insert whole data in one go into the database. So, today we will see, two different ways using that we can insert bulk data into Oracle database table using C#.

This article will explain how we can insert bulk amounts of data in an Oracle database using C#. Generally, what happens is, we insert  records one-by-one into the database, but sometimes, we have requirements to insert numerous amounts of data into the database in one go. This means, rather than going through a one by one process which is a lengthy and time-consuming process, we prefer to insert whole data all at once into the database. So, today we will see two different ways using which we can insert bulk data into Oracle database table using C#.

For this demonstration, we will use SQL Developer Tools for managing our database related tasks and Visual Studio 2017 for creating console applications using C# where we can implement both approaches.

So, let's move to the demonstration and create a table in Oracle database "Test_Db" where we will insert the Bulk Data. Open SQL Developer Tool and create a table name as “BulkInsertTest” with following scripts. The table has three columns as Id, Name, Address. 

 

  1.  CREATE TABLE "TEST_DB"."BULKINSERTTEST"   
  2.   ( "ID" NUMBER NOT NULL ENABLE,   
  3. "NAME" VARCHAR2(200 BYTE) NOT NULL ENABLE,   
  4. "ADDRESS" VARCHAR2(500 BYTE)  
  5.   ) SEGMENT CREATION IMMEDIATE   
  6.  PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255   
  7. NOCOMPRESS LOGGING  
  8.  STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645  
  9.  PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1  
  10.  BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT)  
  11.  TABLESPACE " TEST_DB_DATA" ;  

 

Now we have Oracle table ready. Let’s move to Visual Studio 2017 where we will do a practical demonstration and see both ways of inserting bulk data. So, open Visual Studio with any version [For me, it's Visual Studio 2017] and create a Console Application with a name as "Bulk Insert" with Visual C#. So, we have application ready now.

While working with a database, we must provide the connection string into configuration file to access the database and its table. So, open App.Config inConsole Application and add a connection string inside <appSettings> tag as follows.

 

  1. <appSettings>  
  2.     <add key="connectionString"  
  3.       value="data source=mukesh-pc:1531/mukesh;password=xxxxxxx;user id=mukesh;Connection Timeout=3600;Incr Pool Size=5;Decr Pool Size=2;"/>  
  4. </appSettings>  

 

FIRST WAY TO INSERT BULK DATA

Now the database table is ready, and the connection string is also ready. Let’s right click on solution and click Manage NugGet Packages. It will open a window from where we can install new packages, update packages or see installed packages. Just go to Browse tab and search “Oracle.ManageDataAccess” and install the first one.

We require this package because of performing database operations between C# and Oracle databases. 

FIRST WAY TO INSERT BULK DATA

 

Now move to Program.cs class and add the following code to insert bulk data.

 

  1. using Oracle.ManagedDataAccess.Client;  
  2. using System;  
  3. using System.Configuration;  
  4. using System.Data;  
  5.   
  6. namespace BulkInsert  
  7. {  
  8.     class Program  
  9.     {  
  10.         private readonly string connectionString = ConfigurationManager.AppSettings["connectionString"].ToString();  
  11.         static void Main(string[] args)  
  12.         {  
  13.             Program objProgram = new Program();  
  14.   
  15.             DataTable dt = new DataTable();  
  16.             dt.Columns.Add("ID");  
  17.             dt.Columns.Add("NAME");  
  18.             dt.Columns.Add("ADDRESS");  
  19.   
  20.             for (int i = 0; i < 200000; i++)  
  21.             {  
  22.                 DataRow dr = dt.NewRow();  
  23.                 dr["ID"] = i;  
  24.                 dr["NAME"] = "Hello " + i;  
  25.                 dr["ADDRESS"] = "World " + i;  
  26.   
  27.                 dt.Rows.Add(dr);  
  28.             }  
  29.              
  30.   
  31.             objProgram.SaveUsingOracleBulkCopy(dt);  
  32.         }  
  33.   
  34.           
  35.   
  36.   
  37.         public void SaveUsingOracleBulkCopy(DataTable dt)  
  38.         {  
  39.             try  
  40.             {  
  41.                 using (var connection = new OracleConnection(connectionString))  
  42.                 {  
  43.                     connection.Open();  
  44.                     int[] ids = new int[dt.Rows.Count];  
  45.                     string[] names = new string[dt.Rows.Count];  
  46.                     string[] addresses = new string[dt.Rows.Count];  
  47.   
  48.                     for (int j = 0; j < dt.Rows.Count; j++)  
  49.                     {  
  50.                         ids[j] = Convert.ToInt32(dt.Rows[j]["ID"]);  
  51.                         names[j] = Convert.ToString(dt.Rows[j]["NAME"]);  
  52.                         addresses[j] = Convert.ToString(dt.Rows[j]["ADDRESS"]);  
  53.                     }  
  54.   
  55.                     OracleParameter id = new OracleParameter();  
  56.                     id.OracleDbType = OracleDbType.Int32;  
  57.                     id.Value = ids;  
  58.   
  59.                     OracleParameter name = new OracleParameter();  
  60.                     name.OracleDbType = OracleDbType.Varchar2;  
  61.                     name.Value = names;  
  62.   
  63.                     OracleParameter address = new OracleParameter();  
  64.                     address.OracleDbType = OracleDbType.Varchar2;  
  65.                     address.Value = addresses;  
  66.   
  67.                     // create command and set properties  
  68.                     OracleCommand cmd = connection.CreateCommand();  
  69.                     cmd.CommandText = "INSERT INTO BULKINSERTTEST (ID, NAME, ADDRESS) VALUES (:1, :2, :3)";  
  70.                     cmd.ArrayBindCount = ids.Length;  
  71.                     cmd.Parameters.Add(id);  
  72.                     cmd.Parameters.Add(name);  
  73.                     cmd.Parameters.Add(address);  
  74.                     cmd.ExecuteNonQuery();  
  75.   
  76.                 }  
  77.             }  
  78.             catch (Exception ex)  
  79.             {  
  80.                 throw ex;  
  81.             }  
  82.         }  
  83.     }  
  84. }  

 

First, we have prepared data which needs to be inserted into the database. So, here we are going to create some dummy data using for loop and add it to a data table. So, create a DataTable with three columns as Id, Name, and Address. And add two lakhs record into it inside the Main method using a loop. Now create one method as SaveUsingOracleBulkCopy(DataTable dt) which takes one parameter as a data table. This method will perform a task to insert the data into the database.

Get the connection string and create a connection with Oracle database and open it. Once the connection is available we can perform database operations like insert, update etc. Before adding the data, we have to bind data with Oracle Parameter, so get the data from the data table and give it to Oracle parameter. And at the last, we will write oracle insert command and pass the arguments as a value and finally call the method inside the main method just after preparing the data for the data table.

Now run the code and check your database, you will find that two lakhs record has added inside the BulKInsertTest table.

SECOND WAY TO INSERT BULK DATA

Here we will write minimum code to perform bulk insertion of data into Oracle database where we will only pass the table name in which we want to insert the data and data table that have data. So, just going to use Oracle.DataAccess.dll for creating connection and performing the database operation using CSharp.

NOTE
I am using the same application for both examples. So, I just removed “Oracle.ManageDataAccess” from NuGet before proceeding with the second approach which we have added in the first approach.

NOTE
We can not directly use Oracle.DataAccess.dll, if we do, we will get the following error.  Error:  Could not load file or assembly 'Oracle.DataAccess, Version=2.122.1.0, Culture=neutral, PublicKeyToken=89b483f429c47342' or one of its dependencies.

To resolve the above error, before using Oracle.DataAccess.dll, we have to install Oracle Client Installation at your local system. You can download Oracle client from the following link. Go to the following link and select Oracle version 11 from the dropdown list and Accept the License Agreement and then click to Download.

 

 

Once it will be downloaded as a zip file, just extract the zip file. Once the zip file gets extracted than go to the folder and inside that there is a folder named Install. Now move to install folder, in this folder we have an executable file “oui.exe”. You must run it for installing the Oracle client.

Reference - How to install Oracle Client

Once Oracle client is installed then move to the solution and right click on solution and choose “Manage NuGet Packages”. From the NuGet, you have to search ODP.NET as follows.

 

And install the ODP.NET x86 version. Once it will be installed, give the reference of Oracle.DataAccess.dll from the ODP.NET installed package with your project folder. Once more you must move to Program.cs file and add following code into the class.

 

  1. using System;  
  2. using System.Configuration;  
  3. using System.Data;  
  4.   
  5. namespace BulkInsert  
  6. {  
  7.     class Program  
  8.     {  
  9.         private readonly string connectionString = ConfigurationManager.AppSettings["connectionString"].ToString();  
  10.         static void Main(string[] args)  
  11.         {  
  12.             Program objProgram = new Program();  
  13.   
  14.             DataTable dt = new DataTable();  
  15.             dt.Columns.Add("ID");  
  16.             dt.Columns.Add("NAME");  
  17.             dt.Columns.Add("ADDRESS");  
  18.   
  19.             for (int i = 0; i < 200000; i++)  
  20.             {  
  21.                 DataRow dr = dt.NewRow();  
  22.                 dr["ID"] = i;  
  23.                 dr["NAME"] = "Hello " + i;  
  24.                 dr["ADDRESS"] = "World " + i;  
  25.   
  26.                 dt.Rows.Add(dr);  
  27.             }  
  28.              
  29.             objProgram.SaveUsingOracleBulkCopy("BULKINSERTTEST", dt);  
  30.         }  
  31.   
  32.         public void SaveUsingOracleBulkCopy(string destTableName, DataTable dt)  
  33.         {  
  34.             try  
  35.             {  
  36.                 using (var connection = new Oracle.DataAccess.Client.OracleConnection(connectionString))  
  37.                 {  
  38.                     connection.Open();  
  39.                     using (var bulkCopy = new Oracle.DataAccess.Client.OracleBulkCopy(connection, Oracle.DataAccess.Client.OracleBulkCopyOptions.UseInternalTransaction))  
  40.                     {  
  41.                         bulkCopy.DestinationTableName = destTableName;  
  42.                         bulkCopy.BulkCopyTimeout = 600;  
  43.                         bulkCopy.WriteToServer(dt);  
  44.                     }  
  45.                 }  
  46.             }  
  47.             catch (Exception ex)  
  48.             {  
  49.                 throw ex;  
  50.             }  
  51.         }        
  52.     }  
  53. }  

 

As you can see with the above code, we are using Oracle.DataAccess. It is because in this library we have an available method as “OracleBulkCopy” which inserts whole data in one go without writing a manual script for adding the record. We have to pass only table name and data. It will automatically connect with table and map the columns and finally insert the whole data into one go. For data preparing, we are using the same approach as we have used in the first approach. 

Conclusion

So, today we have seen two different ways to insert bulk data into oracle database using C#.

I hope this post will help you. Please put your feedback using comments which helps me to improve myself for the next post. If you have any doubts please ask your doubts or queries in the comments section and If you like this post, please share it with your friends. Thanks.

Drop here!