diff --git a/Src/Asp.Net/SqlServerTest/Demo/Demo3_Insertable.cs b/Src/Asp.Net/SqlServerTest/Demo/Demo3_Insertable.cs index da3938b36..a8011f2fc 100644 --- a/Src/Asp.Net/SqlServerTest/Demo/Demo3_Insertable.cs +++ b/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().Take(5).ToDataTable(); + dt.TableName = "Order"; + db.Insertable(dt).UseSqlServer().ExecuteBlueCopy(); db.CodeFirst.InitTables(); db.CodeFirst.InitTables(); db.DbMaintenance.TruncateTable("RootTable0"); diff --git a/Src/Asp.Net/SqlSugar/Abstract/InsertableProvider/InsertableProvider.cs b/Src/Asp.Net/SqlSugar/Abstract/InsertableProvider/InsertableProvider.cs index e44311673..6515e02c1 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/InsertableProvider/InsertableProvider.cs +++ b/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; } diff --git a/Src/Asp.Net/SqlSugar/Realization/SqlServer/SqlBuilder/SqlServerBlueCopy.cs b/Src/Asp.Net/SqlSugar/Realization/SqlServer/SqlBuilder/SqlServerBlueCopy.cs index b3fdbaf8f..bf7a1d5f5 100644 --- a/Src/Asp.Net/SqlSugar/Realization/SqlServer/SqlBuilder/SqlServerBlueCopy.cs +++ b/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);