1、OracleBulk程序集引用using ODAC = Oracle.DataAccess.Client
2、有时候不做字段映射,会使导入的数据出错(注意:OracleBulkCopy对索引,事物,主键自增都会有影响,当然没有这些的话相对来说也不错)
简单封装了一下,上代码
1 /// <summary> 2 /// 批量插入数据 3 </summary> 4 <param name="table">数据表</param> 5 <param name="targetTableName">数据库目标表名 6 <returns></returns> 7 public static bool ExcuteBulkData(DataTable table,string targetTableName) 8 { 9 bool result = false; 10 using (ODAC.OracleConnection conn = new ODAC.OracleConnection(connStr)) 11 { 12 using (ODAC.OracleBulkCopy bulkCopy = new ODAC.OracleBulkCopy(connStr,ODAC.OracleBulkCopyOptions.Default)) 13 { 14 if (table != null && table.Rows.Count > 0) 15 { 16 bulkCopy.DestinationTableName = targetTableName; 17 for (int i = 0; i < table.Columns.Count; i++) 18 { 19 string col=table.Columns[i].ColumnName; 20 bulkCopy.ColumnMappings.Add(col,col); 21 } 22 conn.Open(); 23 bulkCopy.WriteToServer(table); 24 result = true; 25 } 26 } 27 } 28 return result; 29 }