为了防止OracleConnection的重复打开和关闭,使用begin end;将sql语句包在里面,然后一次性执行提高插入的效率。
下面代码中要插入的数据在list集合中,如果list集合的count为空,sql为了避免出错,必须在begin和end;之间添加一些东西,下面代码中插入了null;
oracle中的批量插入:

1 StringBuilder strInsSql = new StringBuilder(); 2 strInsSql.Append(" begin "); 3 if (list.Count == 0) {strInsSql.Append(" null; ");} 4 else 5 { 6 for (int i = 0; i < list.Count; i++) 7 { 8 9 strInsSql.Append(" insert into t_parts_apply_refer(apply_id,parts_id,parts_count) values(" + apply_id + "," + list[i].id + "," + list[i].parts_count + "); "); 10 } 11 } 12 13 strInsSql.Append(" end; "); 14 oc.Execute(strInsSql.ToString());