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\DemoE_CodeFirst.cs" />
|
||||||
<Compile Include="Demo\DemoF_Utilities.cs" />
|
<Compile Include="Demo\DemoF_Utilities.cs" />
|
||||||
<Compile Include="Demo\DemoG_SimpleClient.cs" />
|
<Compile Include="Demo\DemoG_SimpleClient.cs" />
|
||||||
|
<Compile Include="Demo\student.cs" />
|
||||||
<Compile Include="Models\Custom.cs" />
|
<Compile Include="Models\Custom.cs" />
|
||||||
<Compile Include="Models\EntityMapper.cs" />
|
<Compile Include="Models\EntityMapper.cs" />
|
||||||
<Compile Include="Models\Mapper.cs" />
|
<Compile Include="Models\Mapper.cs" />
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Data;
|
||||||
|
using System.Data.SqlClient;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
@ -15,29 +17,35 @@ namespace SqlSugar
|
|||||||
public int ExecuteBlueCopy()
|
public int ExecuteBlueCopy()
|
||||||
{
|
{
|
||||||
if (DbColumnInfoList==null||DbColumnInfoList.Count == 0) return 0;
|
if (DbColumnInfoList==null||DbColumnInfoList.Count == 0) return 0;
|
||||||
int pmax = 2030;
|
DataTable dt = new DataTable();
|
||||||
decimal count = DbColumnInfoList.Count;
|
var columns = DbColumnInfoList.First().Select(it => it.DbColumnName ).ToList();
|
||||||
var columns = DbColumnInfoList.First().Select(it => Builder.GetTranslationColumnName(it.DbColumnName)).ToList();
|
foreach (var item in columns)
|
||||||
decimal columnCount = columns.Count();
|
|
||||||
decimal pageSize = count / ((count * columnCount) / pmax);
|
|
||||||
this.Context.Utilities.PageEach(DbColumnInfoList,Convert.ToInt32(pageSize), pageItems =>
|
|
||||||
{
|
{
|
||||||
StringBuilder batchInsetrSql = new StringBuilder();
|
dt.Columns.Add(item);
|
||||||
batchInsetrSql.AppendFormat(InsertBuilder.SqlTemplateBatch, InsertBuilder.GetTableNameString, string.Join(",", columns));
|
}
|
||||||
int i = 0;
|
foreach (var rowInfos in DbColumnInfoList)
|
||||||
foreach (var item in pageItems)
|
{
|
||||||
|
var dr = dt.NewRow();
|
||||||
|
foreach (var item in rowInfos.ToList())
|
||||||
{
|
{
|
||||||
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)))));
|
dr[item.DbColumnName] = item.Value;
|
||||||
if (pageItems.Last() != item)
|
|
||||||
{
|
|
||||||
batchInsetrSql.Append(" UNION ALL");
|
|
||||||
}
|
|
||||||
++i;
|
|
||||||
}
|
}
|
||||||
this.Context.Ado.ExecuteCommand(batchInsetrSql.ToString(),InsertBuilder.Parameters);
|
dt.Rows.Add(dr);
|
||||||
InsertBuilder.Parameters = new List<SugarParameter>();
|
}
|
||||||
});
|
SqlBulkCopy bulkCopy = new SqlBulkCopy(this.Context.Ado.Connection as SqlConnection);
|
||||||
return count.ObjToInt();
|
//获取目标表的名称
|
||||||
|
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)
|
private object AddParameter(int i,string dbColumnName, object value)
|
||||||
|
Loading…
Reference in New Issue
Block a user