Files
SqlSugar/Src/Asp.Net/SqlSugar/Realization/SqlServer/SqlBuilder/SqlServerBlueCopy.cs
2020-12-18 21:13:38 +08:00

120 lines
4.5 KiB
C#

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SqlSugar
{
public class SqlServerBlueCopy
{
internal List<IGrouping<int, DbColumnInfo>> DbColumnInfoList { get; set; }
internal SqlSugarProvider Context { get; set; }
internal ISqlBuilder Builder { get; set; }
internal InsertBuilder InsertBuilder { get; set; }
public int ExecuteBlueCopy()
{
if (DbColumnInfoList == null || DbColumnInfoList.Count == 0) return 0;
DataTable dt;
SqlBulkCopy bulkCopy;
SetCopyData(out dt, out bulkCopy);
try
{
bulkCopy.WriteToServer(dt);
}
catch (Exception ex)
{
this.Context.Ado.Connection.Close();
throw ex;
}
if (this.Context.CurrentConnectionConfig.IsAutoCloseConnection && this.Context.Ado.Transaction == null)
{
this.Context.Ado.Connection.Close();
}
return DbColumnInfoList.Count;
}
public async Task<int> ExecuteBlueCopyAsync()
{
if (DbColumnInfoList == null || DbColumnInfoList.Count == 0) return 0;
DataTable dt;
SqlBulkCopy bulkCopy;
SetCopyData(out dt, out bulkCopy);
try
{
await bulkCopy.WriteToServerAsync(dt);
}
catch (Exception ex)
{
this.Context.Ado.Connection.Close();
throw ex;
}
if (this.Context.CurrentConnectionConfig.IsAutoCloseConnection && this.Context.Ado.Transaction == null)
{
this.Context.Ado.Connection.Close();
}
return DbColumnInfoList.Count;
}
private void SetCopyData(out DataTable dt, out SqlBulkCopy bulkCopy)
{
dt = this.Context.Ado.GetDataTable("select top 0 * from " + InsertBuilder.GetTableNameString);
foreach (var rowInfos in DbColumnInfoList)
{
var dr = dt.NewRow();
foreach (DataColumn item in dt.Columns)
{
var rows = rowInfos.ToList();
var value = rows.FirstOrDefault(it =>
it.DbColumnName.Equals(item.ColumnName, StringComparison.CurrentCultureIgnoreCase) ||
it.PropertyName.Equals(item.ColumnName, StringComparison.CurrentCultureIgnoreCase)
);
if (value != null)
{
if (value.Value != null && UtilMethods.GetUnderType(value.Value.GetType()) == UtilConstants.DateType)
{
if (value.Value != null && value.Value.ToString() == DateTime.MinValue.ToString())
{
value.Value = Convert.ToDateTime("1753/01/01");
}
}
if (value.Value == null)
{
value.Value = DBNull.Value;
}
dr[item.ColumnName] = value.Value;
}
}
dt.Rows.Add(dr);
}
bulkCopy = null;
if (this.Context.Ado.Transaction != null)
{
var sqlTran = this.Context.Ado.Transaction as SqlTransaction;
bulkCopy = new SqlBulkCopy(this.Context.Ado.Connection as SqlConnection, SqlBulkCopyOptions.CheckConstraints, sqlTran);
}
else
{
bulkCopy = new SqlBulkCopy(this.Context.Ado.Connection as SqlConnection);
}
//获取目标表的名称
bulkCopy.DestinationTableName = InsertBuilder.GetTableNameString;
//写入DataReader对象
if (this.Context.Ado.Connection.State == ConnectionState.Closed)
{
this.Context.Ado.Connection.Open();
}
}
private object AddParameter(int i,string dbColumnName, object value)
{
var name =Builder.SqlParameterKeyWord+dbColumnName+i;
InsertBuilder.Parameters.Add(new SugarParameter(name,value));
return name;
}
}
}