mirror of
https://github.com/nsnail/FreeSql.git
synced 2025-06-18 20:08:15 +08:00
- 增加 IInsert.ExecutePgCopy 扩展方法执行 PostgreSQL Copy 批量导入,在 FreeSql.Provider.PostgreSQL 可用;
This commit is contained in:
@ -4,7 +4,6 @@ using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Data.Common;
|
||||
using System.Data.SqlClient;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
@ -20,8 +19,8 @@ namespace FreeSql.SqlServer.Curd
|
||||
}
|
||||
|
||||
internal IFreeSql InternalOrm => _orm as IFreeSql;
|
||||
internal SqlConnection InternalConnection => _connection as SqlConnection;
|
||||
internal SqlTransaction InternalTransaction => _transaction as SqlTransaction;
|
||||
internal DbConnection InternalConnection => _connection;
|
||||
internal DbTransaction InternalTransaction => _transaction;
|
||||
|
||||
public override int ExecuteAffrows() => base.SplitExecuteAffrows(_batchValuesLimit > 0 ? _batchValuesLimit : 1000, _batchParameterLimit > 0 ? _batchParameterLimit : 2100);
|
||||
public override long ExecuteIdentity() => base.SplitExecuteIdentity(_batchValuesLimit > 0 ? _batchValuesLimit : 1000, _batchParameterLimit > 0 ? _batchParameterLimit : 2100);
|
||||
|
@ -78,50 +78,61 @@ public static partial class FreeSqlSqlServerGlobalExtensions
|
||||
bulkCopy.WriteToServer(dt);
|
||||
};
|
||||
|
||||
if (insert.InternalConnection == null && insert.InternalTransaction == null)
|
||||
try
|
||||
{
|
||||
using (var conn = insert.InternalOrm.Ado.MasterPool.Get())
|
||||
if (insert.InternalConnection == null && insert.InternalTransaction == null)
|
||||
{
|
||||
using (var conn = insert.InternalOrm.Ado.MasterPool.Get())
|
||||
{
|
||||
using (var bulkCopy = copyOptions == SqlBulkCopyOptions.Default ?
|
||||
new SqlBulkCopy(conn.Value as SqlConnection) :
|
||||
new SqlBulkCopy(conn.Value as SqlConnection, copyOptions, null))
|
||||
{
|
||||
writeToServer(bulkCopy);
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (insert.InternalTransaction != null)
|
||||
{
|
||||
using (var bulkCopy = copyOptions == SqlBulkCopyOptions.Default ?
|
||||
new SqlBulkCopy(conn.Value as SqlConnection) :
|
||||
new SqlBulkCopy(conn.Value as SqlConnection, copyOptions, null))
|
||||
new SqlBulkCopy(insert.InternalTransaction.Connection as SqlConnection) :
|
||||
new SqlBulkCopy(insert.InternalTransaction.Connection as SqlConnection, copyOptions, insert.InternalTransaction as SqlTransaction))
|
||||
{
|
||||
writeToServer(bulkCopy);
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (insert.InternalTransaction != null)
|
||||
{
|
||||
using (var bulkCopy = copyOptions == SqlBulkCopyOptions.Default ?
|
||||
new SqlBulkCopy(insert.InternalTransaction.Connection) :
|
||||
new SqlBulkCopy(insert.InternalTransaction.Connection, copyOptions, insert.InternalTransaction))
|
||||
else if (insert.InternalConnection != null)
|
||||
{
|
||||
writeToServer(bulkCopy);
|
||||
var conn = insert.InternalConnection as SqlConnection;
|
||||
var isNotOpen = false;
|
||||
if (conn.State != System.Data.ConnectionState.Open)
|
||||
{
|
||||
isNotOpen = true;
|
||||
conn.Open();
|
||||
}
|
||||
try
|
||||
{
|
||||
using (var bulkCopy = copyOptions == SqlBulkCopyOptions.Default ?
|
||||
new SqlBulkCopy(conn) :
|
||||
new SqlBulkCopy(conn, copyOptions, null))
|
||||
{
|
||||
writeToServer(bulkCopy);
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (isNotOpen)
|
||||
conn.Close();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new NotImplementedException("ExecuteSqlBulkCopy 未实现错误,请反馈给作者");
|
||||
}
|
||||
}
|
||||
else if (insert.InternalConnection != null)
|
||||
finally
|
||||
{
|
||||
var conn = insert.InternalConnection;
|
||||
var isNotOpen = false;
|
||||
if (conn.State != System.Data.ConnectionState.Open)
|
||||
{
|
||||
isNotOpen = true;
|
||||
conn.Open();
|
||||
}
|
||||
using (var bulkCopy = copyOptions == SqlBulkCopyOptions.Default ?
|
||||
new SqlBulkCopy(insert.InternalConnection) :
|
||||
new SqlBulkCopy(insert.InternalConnection, copyOptions, null))
|
||||
{
|
||||
writeToServer(bulkCopy);
|
||||
}
|
||||
if (isNotOpen)
|
||||
{
|
||||
conn.Close();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new NotImplementedException("未实现错误,请反馈给作者");
|
||||
dt.Clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user