mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-07-15 14:04:44 +08:00
Update blukcopy
This commit is contained in:
parent
614b9971a3
commit
7e6672f7e3
@ -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");
|
||||
|
@ -239,6 +239,7 @@ namespace SqlSugar
|
||||
result.InsertBuilder = this.InsertBuilder;
|
||||
result.Builder = this.SqlBuilder;
|
||||
result.Context = this.Context;
|
||||
result.Inserts=this.InsertObjs;
|
||||
return result;
|
||||
}
|
||||
|
||||
|
@ -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…
Reference in New Issue
Block a user