mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-06-28 13:34:32 +08:00
Update SqlblueCopy
This commit is contained in:
parent
6e4c6a7ff7
commit
61295f3487
@ -67,6 +67,7 @@
|
||||
<Compile Include="Demo\DemoE_CodeFirst.cs" />
|
||||
<Compile Include="Demo\DemoF_Utilities.cs" />
|
||||
<Compile Include="Demo\DemoG_SimpleClient.cs" />
|
||||
<Compile Include="Demo\student.cs" />
|
||||
<Compile Include="Models\Custom.cs" />
|
||||
<Compile Include="Models\EntityMapper.cs" />
|
||||
<Compile Include="Models\Mapper.cs" />
|
||||
|
@ -1,5 +1,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Data.SqlClient;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
@ -15,29 +17,35 @@ namespace SqlSugar
|
||||
public int ExecuteBlueCopy()
|
||||
{
|
||||
if (DbColumnInfoList==null||DbColumnInfoList.Count == 0) return 0;
|
||||
int pmax = 2030;
|
||||
decimal count = DbColumnInfoList.Count;
|
||||
var columns = DbColumnInfoList.First().Select(it => Builder.GetTranslationColumnName(it.DbColumnName)).ToList();
|
||||
decimal columnCount = columns.Count();
|
||||
decimal pageSize = count / ((count * columnCount) / pmax);
|
||||
this.Context.Utilities.PageEach(DbColumnInfoList,Convert.ToInt32(pageSize), pageItems =>
|
||||
DataTable dt = new DataTable();
|
||||
var columns = DbColumnInfoList.First().Select(it => it.DbColumnName ).ToList();
|
||||
foreach (var item in columns)
|
||||
{
|
||||
StringBuilder batchInsetrSql = new StringBuilder();
|
||||
batchInsetrSql.AppendFormat(InsertBuilder.SqlTemplateBatch, InsertBuilder.GetTableNameString, string.Join(",", columns));
|
||||
int i = 0;
|
||||
foreach (var item in pageItems)
|
||||
{
|
||||
batchInsetrSql.Append("\r\n SELECT " + string.Join(",", item.ToList().Select(it => string.Format(InsertBuilder.SqlTemplateBatchSelect, AddParameter(i,it.DbColumnName,it.Value), Builder.GetTranslationColumnName(it.DbColumnName)))));
|
||||
if (pageItems.Last() != item)
|
||||
{
|
||||
batchInsetrSql.Append(" UNION ALL");
|
||||
dt.Columns.Add(item);
|
||||
}
|
||||
++i;
|
||||
foreach (var rowInfos in DbColumnInfoList)
|
||||
{
|
||||
var dr = dt.NewRow();
|
||||
foreach (var item in rowInfos.ToList())
|
||||
{
|
||||
dr[item.DbColumnName] = item.Value;
|
||||
}
|
||||
this.Context.Ado.ExecuteCommand(batchInsetrSql.ToString(),InsertBuilder.Parameters);
|
||||
InsertBuilder.Parameters = new List<SugarParameter>();
|
||||
});
|
||||
return count.ObjToInt();
|
||||
dt.Rows.Add(dr);
|
||||
}
|
||||
SqlBulkCopy bulkCopy = new SqlBulkCopy(this.Context.Ado.Connection as SqlConnection);
|
||||
//获取目标表的名称
|
||||
bulkCopy.DestinationTableName = InsertBuilder.EntityInfo.EntityName;
|
||||
//写入DataReader对象
|
||||
if (this.Context.Ado.Connection.State == ConnectionState.Closed)
|
||||
{
|
||||
this.Context.Ado.Connection.Open();
|
||||
}
|
||||
bulkCopy.WriteToServer(dt);
|
||||
if (this.Context.CurrentConnectionConfig.IsAutoCloseConnection && this.Context.Ado.Transaction == null)
|
||||
{
|
||||
this.Context.Ado.Connection.Close();
|
||||
}
|
||||
return DbColumnInfoList.Count;
|
||||
}
|
||||
|
||||
private object AddParameter(int i,string dbColumnName, object value)
|
||||
|
Loading…
Reference in New Issue
Block a user