Browse Source

Update blukcopy

pull/1/head
skx 4 years ago
parent
commit
7e6672f7e3
  1. 4
      Src/Asp.Net/SqlServerTest/Demo/Demo3_Insertable.cs
  2. 1
      Src/Asp.Net/SqlSugar/Abstract/InsertableProvider/InsertableProvider.cs
  3. 37
      Src/Asp.Net/SqlSugar/Realization/SqlServer/SqlBuilder/SqlServerBlueCopy.cs

4
Src/Asp.Net/SqlServerTest/Demo/Demo3_Insertable.cs

@ -55,7 +55,9 @@ namespace OrmTest
new Order() { Id = 12, Name = "order12" , Price=20, CreateTime=DateTime.Now, CustomId=1}
};
db.Insertable(insertObjs).UseSqlServer().ExecuteBlueCopy();
var dt = db.Queryable<Order>().Take(5).ToDataTable();
dt.TableName = "Order";
db.Insertable(dt).UseSqlServer().ExecuteBlueCopy();
db.CodeFirst.InitTables<RootTable0, TwoItem, TwoItem2, TwoItem3>();
db.CodeFirst.InitTables<ThreeItem2>();
db.DbMaintenance.TruncateTable("RootTable0");

1
Src/Asp.Net/SqlSugar/Abstract/InsertableProvider/InsertableProvider.cs

@ -239,6 +239,7 @@ namespace SqlSugar
result.InsertBuilder = this.InsertBuilder;
result.Builder = this.SqlBuilder;
result.Context = this.Context;
result.Inserts=this.InsertObjs;
return result;
}

37
Src/Asp.Net/SqlSugar/Realization/SqlServer/SqlBuilder/SqlServerBlueCopy.cs

@ -14,10 +14,17 @@ namespace SqlSugar
internal SqlSugarProvider Context { get; set; }
internal ISqlBuilder Builder { get; set; }
internal InsertBuilder InsertBuilder { get; set; }
public object[] Inserts { get; internal set; }
public int ExecuteBlueCopy()
{
if (DbColumnInfoList == null || DbColumnInfoList.Count == 0) return 0;
if (Inserts.First().GetType() == typeof(DataTable))
{
return WriteToServer();
}
DataTable dt;
SqlBulkCopy bulkCopy;
SetCopyData(out dt, out bulkCopy);
@ -40,6 +47,11 @@ namespace SqlSugar
{
if (DbColumnInfoList == null || DbColumnInfoList.Count == 0) return 0;
if (Inserts.First().GetType() == typeof(DataTable))
{
return WriteToServer();
}
DataTable dt;
SqlBulkCopy bulkCopy;
SetCopyData(out dt, out bulkCopy);
@ -59,6 +71,31 @@ namespace SqlSugar
return DbColumnInfoList.Count;
}
private int WriteToServer()
{
SqlBulkCopy copy;
if (this.Context.Ado.Transaction == null)
{
copy = new SqlBulkCopy((SqlConnection)this.Context.Ado.Connection);
}
else
{
copy = new SqlBulkCopy((SqlConnection)this.Context.Ado.Connection, SqlBulkCopyOptions.CheckConstraints, (SqlTransaction)this.Context.Ado.Transaction);
}
var dt = this.Inserts.First() as DataTable;
if (dt == null)
{
return 0;
}
if (this.Context.Ado.Connection.State == ConnectionState.Closed)
{
this.Context.Ado.Connection.Open();
}
Check.Exception(dt.TableName=="Table","dt.TableName can't be null ");
copy.DestinationTableName =this.Builder.GetTranslationColumnName(dt.TableName);
copy.WriteToServer(dt);
return dt.Rows.Count;
}
private void SetCopyData(out DataTable dt, out SqlBulkCopy bulkCopy)
{
dt = this.Context.Ado.GetDataTable("select top 0 * from " + InsertBuilder.GetTableNameString);

Loading…
Cancel
Save