Update db.Fastest

This commit is contained in:
sunkaixuna
2021-11-19 19:11:36 +08:00
parent d3b8ecaf4d
commit 135f3ab53d
3 changed files with 68 additions and 2 deletions

View File

@@ -9,12 +9,12 @@ namespace SqlSugar
{ {
public partial class FastestProvider<T> : IFastest<T> where T : class, new() public partial class FastestProvider<T> : IFastest<T> where T : class, new()
{ {
private SqlServerFastBuilder GetBuider() private IFastBuilder GetBuider()
{ {
switch (this.context.CurrentConnectionConfig.DbType) switch (this.context.CurrentConnectionConfig.DbType)
{ {
case DbType.MySql: case DbType.MySql:
break; return new MySqlFastBuilder();
case DbType.SqlServer: case DbType.SqlServer:
return new SqlServerFastBuilder(); return new SqlServerFastBuilder();
case DbType.Sqlite: case DbType.Sqlite:

View File

@@ -0,0 +1,65 @@
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SqlSugar
{
public class MySqlFastBuilder:FastBuilder,IFastBuilder
{
public async Task<int> ExecuteBulkCopyAsync(DataTable dt)
{
var dllPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "failFiles");
DirectoryInfo dir = new DirectoryInfo(dllPath);
if (!dir.Exists)
{
dir.Create();
}
var fileName = Path.Combine(dllPath, Guid.NewGuid().ToString() + ".csv");
var dataTableToCsv =new MySqlBlukCopy<object>(this.Context.Context,null,null).DataTableToCsvString(dt);
File.WriteAllText(fileName, dataTableToCsv, new UTF8Encoding(false));
MySqlConnection conn = this.Context.Ado.Connection as MySqlConnection;
int result = 0;
try
{
this.Context.Ado.Open();
// IsolationLevel.Parse
MySqlBulkLoader bulk = new MySqlBulkLoader(conn)
{
CharacterSet = "UTF8",
FieldTerminator = ",",
FieldQuotationCharacter = '"',
EscapeCharacter = '"',
LineTerminator = "\r\n",
FileName = fileName,
NumberOfLinesToSkip = 0,
TableName = dt.TableName,
Local = true,
};
bulk.Columns.AddRange(dt.Columns.Cast<DataColumn>().Select(colum => colum.ColumnName).Distinct().ToArray());
result= await bulk.LoadAsync();
//执行成功才删除文件
if (File.Exists(fileName))
{
File.Delete(fileName);
}
}
catch (MySqlException ex)
{
throw ex;
}
finally
{
CloseDb();
}
return result;
}
}
}

View File

@@ -95,6 +95,7 @@
<Compile Include="Abstract\FilterProvider\FilterProvider.cs" /> <Compile Include="Abstract\FilterProvider\FilterProvider.cs" />
<Compile Include="Interface\IFastBuilder.cs" /> <Compile Include="Interface\IFastBuilder.cs" />
<Compile Include="Interface\IFastest.cs" /> <Compile Include="Interface\IFastest.cs" />
<Compile Include="Realization\MySql\SqlBuilder\MySqlFastBuilder.cs" />
<Compile Include="Realization\SqlServer\SqlBuilder\SqlServerFastBuilder.cs" /> <Compile Include="Realization\SqlServer\SqlBuilder\SqlServerFastBuilder.cs" />
<Compile Include="SpliteTable\SplitTableAttribute.cs" /> <Compile Include="SpliteTable\SplitTableAttribute.cs" />
<Compile Include="Abstract\InsertableProvider\InsertableProvider.cs" /> <Compile Include="Abstract\InsertableProvider\InsertableProvider.cs" />