Oracle中批量插入

为了防止OracleConnection的重复打开和关闭,使用begin end;将sql语句包在里面,然后一次性执行提高插入的效率。

下面代码中要插入的数据在list集合中,如果list集合的count为空,sql为了避免出错,必须在begin和end;之间添加一些东西,下面代码中插入了null;

oracle中的批量插入:

Add
 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());

作者:Cboii

本博客所有文章仅用于学习、研究和交流目的,欢迎非商业性质转载。

由于博主的水平不高,不足和错误之处在所难免,希望大家能够批评指出。

在wordpress安装、主题、插件以及开发上面有问题的,可以加入qq群:1140958614(Wp建站每日学习/交流群)进行学习和提问

如果需要建站服务,可以直接联系我的qq:185369045

原文地址:https://www.cnblogs.com/chenboyi081/p/4044854.html