之前写过一次批量写入的功能,但是,这次还是踩坑了,记录一下:
Oracle.DataAccess 本地调试的时候,目标平台要调整为x86,才可顺利执行,放到服务器上的时候要改为x64位。
public static bool BulkCopyToOraDB(DataTable dt,string connectionString) { try { System.Diagnostics.Debug.WriteLine("表名稱-----"+dt.TableName+","+DateTime.Now.ToString(yyyyMMddHHmmss.fff)); Oracle.DataAccess.Client.OracleConnection conn = new Oracle.DataAccess.Client.OracleConnection(connectionString); OracleBulkCopy bulkCopy = new OracleBulkCopy(connectionString, OracleBulkCopyOptions.UseInternalTransaction); bulkCopy.BatchSize = 100000; bulkCopy.BulkCopyTimeout = 180; bulkCopy.DestinationTableName = dt.TableName; //目标表的名称 bulkCopy.BatchSize = dt.Rows.Count; //每一批次中的行数 try { conn.Open(); if (dt != null && dt.Rows.Count != 0) bulkCopy.WriteToServer(dt); //将提供的数据源中的所有行复制到目标表中 } catch (Exception ex) { throw ex; } finally { conn.Close(); if (bulkCopy != null) bulkCopy.Close(); } System.Diagnostics.Debug.WriteLine("共添加:" + dt.Rows.Count + "條數數據," + DateTime.Now.ToString(yyyyMMddHHmmss.fff)); return true; } catch (Exception ex) { throw ex; } }