PetaPoco 批量插入数据
网上找的代码,还没经过验证
/// <summary>
/// Bulk inserts multiple rows to SQL
/// </summary>
/// <param name="tableName">The name of the table to insert into</param>
/// <param name="primaryKeyName">The name of the primary key column of the table</param>
/// <param name="autoIncrement">True if the primary key is automatically allocated by the DB</param>
/// <param name="pocos">The POCO objects that specifies the column values to be inserted</param>
/// <param name="batchSize">The number of POCOS to be grouped together for each database rounddtrip</param>
public void BulkInsert(string tableName, string primaryKeyName, bool autoIncrement, IEnumerable<object> pocos, int batchSize = )
{
try
{
OpenSharedConnection();
try
{
using (var cmd = CreateCommand(_sharedConnection, ""))
{
var pd = PocoData.ForObject(pocos.First(), primaryKeyName);
// Create list of columnnames only once
var names = new List<string>();
foreach (var i in pd.Columns)
{
// Don't insert result columns
if (i.Value.ResultColumn)
continue; // Don't insert the primary key (except under oracle where we need bring in the next sequence value)
if (autoIncrement && primaryKeyName != null && string.Compare(i.Key, primaryKeyName, true) == )
{
// Setup auto increment expression
string autoIncExpression = _dbType.GetAutoIncrementExpression(pd.TableInfo);
if (autoIncExpression != null)
{
names.Add(i.Key);
}
continue;
}
names.Add(_dbType.EscapeSqlIdentifier(i.Key));
}
var namesArray = names.ToArray(); var values = new List<string>();
int count = ;
do
{
cmd.CommandText = "";
cmd.Parameters.Clear();
var index = ;
foreach (var poco in pocos.Skip(count).Take(batchSize))
{
values.Clear();
foreach (var i in pd.Columns)
{
// Don't insert result columns
if (i.Value.ResultColumn) continue; // Don't insert the primary key (except under oracle where we need bring in the next sequence value)
if (autoIncrement && primaryKeyName != null && string.Compare(i.Key, primaryKeyName, true) == )
{
// Setup auto increment expression
string autoIncExpression = _dbType.GetAutoIncrementExpression(pd.TableInfo);
if (autoIncExpression != null)
{
values.Add(autoIncExpression);
}
continue;
} values.Add(string.Format("{0}{1}", _paramPrefix, index++));
AddParam(cmd, i.Value.GetValue(poco), i.Value.PropertyInfo);
} string outputClause = String.Empty;
if (autoIncrement)
{
outputClause = _dbType.GetInsertOutputClause(primaryKeyName);
} cmd.CommandText += string.Format("INSERT INTO {0} ({1}){2} VALUES ({3})", _dbType.EscapeTableName(tableName),
string.Join(",", namesArray), outputClause, string.Join(",", values.ToArray()));
}
// Are we done?
if (cmd.CommandText == "") break;
count += batchSize;
DoPreExecute(cmd);
cmd.ExecuteNonQuery();
OnExecutedCommand(cmd);
}
while (true); }
}
finally
{
CloseSharedConnection();
}
}
catch (Exception x)
{
if (OnException(x))
throw;
}
} /// <summary>
/// Performs a SQL Bulk Insert
/// </summary>
/// <param name="pocos">The POCO objects that specifies the column values to be inserted</param>
/// <param name="batchSize">The number of POCOS to be grouped together for each database rounddtrip</param>
public void BulkInsert(IEnumerable<object> pocos, int batchSize = )
{
if (!pocos.Any()) return;
var pd = PocoData.ForType(pocos.First().GetType());
BulkInsert(pd.TableInfo.TableName, pd.TableInfo.PrimaryKey, pd.TableInfo.AutoIncrement, pocos);
}
摘自:http://stackoverflow.com/questions/6595105/bulk-insert-update-with-petapoco/14479073
PetaPoco 批量插入数据的更多相关文章
- PetaPoco批量插入数据
VS添加完组件,自动生成的PetaPoco.cs文件中没有SqlBulkInsert这个方法,但是可以在里面添加,代码如下: /// <summary> /// BulkInsert // ...
- C#批量插入数据到Sqlserver中的四种方式
我的新书ASP.NET MVC企业级实战预计明年2月份出版,感谢大家关注! 本篇,我将来讲解一下在Sqlserver中批量插入数据. 先创建一个用来测试的数据库和表,为了让插入数据更快,表中主键采用的 ...
- .Net批量插入数据到SQLServer数据库,System.Data.SqlClient.SqlBulkCopy类批量插入大数据到数据库
批量的的数据导入数据库中,尽量少的访问数据库,高性能的对数据库进行存储. 采用SqlBulkCopy来处理存储数据.SqlBulkCopy存储大批量的数据非常的高效,将内存中的数据表直接的一次性的存储 ...
- sql 中的Bulk和C# 中的SqlBulkCopy批量插入数据 ( 回顾 and 粗谈 )
通常,我们会对于一个文本文件数据导入到数据库中,不多说,上代码. 首先,表结构如下. 其次,在我当前D盘中有个文本文件名为2.txt的文件. 在数据库中,可以这样通过一句代码插入. Bulk in ...
- Java 批量插入数据(Oracle)
//批量添加20000条数据用时8秒. try { String url = "jdbc:oracle:thin:@IP:1521:orcl"; // orcl为数据库的SI ...
- 批量插入数据(基于Mybatis的实现-Oracle)
前言:做一个数据同步项目,要求:同步数据不丢失的情况下,提高插入性能. 项目DB框架:Mybatis.DataBase:Oracle. -------------------------------- ...
- C#批量插入数据到Sqlserver中的三种方式
本篇,我将来讲解一下在Sqlserver中批量插入数据. 先创建一个用来测试的数据库和表,为了让插入数据更快,表中主键采用的是GUID,表中没有创建任何索引.GUID必然是比自增长要快的,因为你生 成 ...
- SqlServer——批量插入数据
像Major表里面批量插入数据演示: 代码如下: Declare @I int Set @I= Begin Tran InsertData: Insert into Major values(@I,' ...
- mybatis批量插入数据到oracle
mybatis 批量插入数据到oracle报 ”java.sql.SQLException: ORA-00933: SQL 命令未正确结束“ 错误解决方法 oracle批量插入使用 insert a ...
随机推荐
- 惯性导航之MEMS加速度计原理
一 加速度计原理 1.1 加速度计由MEMS传感器与信号处理芯片组成. 1.2 MEMS加速度计工作原理 由上电容.中电容板(可移动).下电容板等组成:当加速度达到一定值后,中电容板会移动,与上.下电 ...
- install graph-tool
try this if ubuntu version is >= 14.04 sudo apt-get update sudo apt-get upgrade sudo apt-get -y i ...
- Type.GetType()反射另外项目中的类时返回null的解决方法
项目1:ProjectA namespace ProjectA { public class paa { .... } } Type.GetType("paa")返回null Ty ...
- log4j配置生成日志保存在数据库
利用MDC可以存储参数,MDC原理:相当于一个map将值存储起来,调用时可以根据key将自定义的在值存入对应位置(数据库或文件等).使用: 配置文件:log4j.properties ### dire ...
- 【杂】孔明锁6根解法 & 九连环的拆卸方法及还原
**************************** Part1: 孔明锁6根解法: **************************** 第一步,编号: 第二步,按照编号组装: 第三步,完成 ...
- Cannot forward after response has been committed
项目:蒙文词语检索 日期:2016-05-01 提示:Cannot forward after response has been committed 出处:request.getRequestDis ...
- REVERSE-DAILY(5)-rev2
比较有意思的一道题目 提示是一个迷宫 链接: http://pan.baidu.com/s/1pLhINBt 密码:91bs 先运行程序,要求是输入字符串,错误会输出"Sorry you ...
- vb小程序浅析
系统 : Windows xp 程序 : BJCM10B 程序下载地址 :http://pan.baidu.com/s/1dFyXe29 要求 : 编写注册机 使用工具 : OD 可在看雪论坛中查找关 ...
- secureCRT的一些小知识
secureCRT 是一个非常不错的终端软件,在嵌入式开发过程中经常使用到,所以了解一下其快捷键操作是非常有必要的,可以提高开发效率. 0.在secureCRT里切换不同的窗口:ctrl+tab. ...
- Android手机刷机失败的自救方法
刷机对于一些android手机的高级用户来说已经是家常便饭了,很多新手也都跟着教程轻松了学会刷机.升级系统,也都开始经常在网上搜罗一些自制的系统进行刷机,体验新系统带来的新感觉.但是有句古话叫常在河边 ...