Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a Bulk Insert using Oracle Managed Data Acess c#

I have a dynamic list object that i want to BulkInsert into my DB, i was using the old OracleBulkCopy method from another lib, but i can't use that lib anymore and on the new lib i don't have this method.

New lib : using Oracle.ManagedDataAccess.Client;

Old lib : Oracle.DataAccess.Client

Does anyone know a easy way to do the Bulk without creating lists or arrays to do it?

like image 522
Lucio Zenir Avatar asked Oct 15 '25 20:10

Lucio Zenir


1 Answers

The Oracle.ManagedDataAccess.Client lib doesn't yet support BulkCopy.

You can compare functionality from both libs in the folowing link: Oracle Managed Driver Comparison

Related Question

Another option would be to use Array Binding.

Example:

using Oracle.ManagedDataAccess.Client;

namespace ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            string connString = "Data Source=xyz; user id=**; password=**";
            using (var con = new OracleConnection(connString))
            {
                con.Open();
                int[] foos = new int[3] { 1, 2, 3 };
                string[] bars = new string[3] { "A", "B", "C" };

                OracleParameter pFoo = new OracleParameter();
                pFoo.OracleDbType = OracleDbType.Int32;
                pFoo.Value = foos;

                OracleParameter pBar = new OracleParameter();
                pBar.OracleDbType = OracleDbType.Varchar2;
                pBar.Value = bars;

                // create command and set properties
                OracleCommand cmd = con.CreateCommand();
                cmd.CommandText = "insert into test (foo, bar) values (:1, :2)";
                cmd.ArrayBindCount = foos.Length;
                cmd.Parameters.Add(pFoo);
                cmd.Parameters.Add(pBar);
                cmd.ExecuteNonQuery();
            }
        }
    }
}
like image 184
Mateus Schneiders Avatar answered Oct 18 '25 10:10

Mateus Schneiders



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!