博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
PetaPoco 批量插入数据
阅读量:4698 次
发布时间:2019-06-09

本文共 5298 字,大约阅读时间需要 17 分钟。

网上找的代码,还没经过验证

///     /// Bulk inserts multiple rows to SQL    ///     /// The name of the table to insert into    /// The name of the primary key column of the table    /// True if the primary key is automatically allocated by the DB    /// The POCO objects that specifies the column values to be inserted    /// The number of POCOS to be grouped together for each database rounddtrip            public void BulkInsert(string tableName, string primaryKeyName, bool autoIncrement, IEnumerable pocos, int batchSize = 25)    {        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
(); 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) == 0) { // 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
(); int count = 0; do { cmd.CommandText = ""; cmd.Parameters.Clear(); var index = 0; 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) == 0) { // 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; } } ///
/// Performs a SQL Bulk Insert /// ///
The POCO objects that specifies the column values to be inserted ///
The number of POCOS to be grouped together for each database rounddtrip public void BulkInsert(IEnumerable
pocos, int batchSize = 25) { 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

转载于:https://www.cnblogs.com/haight/p/5208682.html

你可能感兴趣的文章
安全漏洞之Java
查看>>
Oracle 组函数count()
查看>>
Session的使用过程中应注意的一个小问题
查看>>
SDK,API,DLL名词解释
查看>>
试探算法
查看>>
jquery.validation.js 使用
查看>>
数据库高级查询
查看>>
C语言实现封装、继承和多态
查看>>
创建文件
查看>>
Nginx 相关介绍
查看>>
leetcode[33]Search in Rotated Sorted Array
查看>>
OpenCV Shi-Tomasi角点检测子
查看>>
eval(PHP 4, PHP 5)
查看>>
readelf用法小记
查看>>
Java中JavaScript unescape与escape函数算法
查看>>
js的基础要点
查看>>
C#/IOS/Android通用加密解密方法
查看>>
Web API 简单示例
查看>>
返璞归真 asp.net mvc (4) - View/ViewEngine
查看>>
ADO.Net对Oracle数据库的操作【转载】
查看>>