C# OracleBulkCopy batch insert oracle database method

It can only be used on machines with oracle 11G client installed. OracleDataAccess.DLL in the ODP.NET component is used, and the namespace reference is Oracle.DataAccess.Client;

 

#region Insert data in batches
         ///  <summary> 
        /// Insert data in batches
         ///  </summary> 
        ///  <param name="dt"> Data to be inserted </param> 
        ///  <param name= "targetTable"> Table in the database </param> 
        public  static  void BulkToDB(DataTable dt, string targetTable)
        {
            OracleConnection conn = new OracleConnection(connOrcleString);
            OracleBulkCopy bulkCopy = new OracleBulkCopy(connOrcleString, OracleBulkCopyOptions.UseInternalTransaction);    // Use data from other sources to effectively bulk load Oracle tables
             // conn.BeginTransaction();
             // OracleBulkCopy bulkCopy = new OracleBulkCopy(connOrcleString, OracleBulkCopyOptions.Default); 
            bulkCopy .BatchSize = 100000 ;
            bulkCopy.BulkCopyTimeout = 260;
            bulkCopy.DestinationTableName = targetTable;     // Name of the target table on the server 
            bulkCopy.BatchSize = dt.Rows.Count;    // Number of rows in each batch 
            try
            {
                conn.Open();
                if (dt != null && dt.Rows.Count != 0)
                   
                    bulkCopy.WriteToServer(dt);    // Copy all rows from the provided data source to the target table 
            }
             catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                conn.Close();
                if (bulkCopy != null)
                    bulkCopy.Close();
            }
        }
        #endregion

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325476173&siteId=291194637