Browse Source

Update db.Fastest

pull/10/head
sunkaixuna 3 years ago
parent
commit
135f3ab53d
  1. 4
      Src/Asp.Net/SqlSugar/Abstract/FastestProvider/Private.cs
  2. 65
      Src/Asp.Net/SqlSugar/Realization/MySql/SqlBuilder/MySqlFastBuilder.cs
  3. 1
      Src/Asp.Net/SqlSugar/SqlSugar.csproj

4
Src/Asp.Net/SqlSugar/Abstract/FastestProvider/Private.cs

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

65
Src/Asp.Net/SqlSugar/Realization/MySql/SqlBuilder/MySqlFastBuilder.cs

@ -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;
}
}
}

1
Src/Asp.Net/SqlSugar/SqlSugar.csproj

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

Loading…
Cancel
Save