diff --git a/Src/Asp.Net/SqlSugar/Abstract/FastestProvider/Private.cs b/Src/Asp.Net/SqlSugar/Abstract/FastestProvider/Private.cs index 289bde247..0eda6fa62 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/FastestProvider/Private.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/FastestProvider/Private.cs @@ -9,12 +9,12 @@ namespace SqlSugar { public partial class FastestProvider : IFastest 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: diff --git a/Src/Asp.Net/SqlSugar/Realization/MySql/SqlBuilder/MySqlFastBuilder.cs b/Src/Asp.Net/SqlSugar/Realization/MySql/SqlBuilder/MySqlFastBuilder.cs new file mode 100644 index 000000000..8df3b2942 --- /dev/null +++ b/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 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(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().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; + } + } +} diff --git a/Src/Asp.Net/SqlSugar/SqlSugar.csproj b/Src/Asp.Net/SqlSugar/SqlSugar.csproj index ce16935bf..e81a070ea 100644 --- a/Src/Asp.Net/SqlSugar/SqlSugar.csproj +++ b/Src/Asp.Net/SqlSugar/SqlSugar.csproj @@ -95,6 +95,7 @@ +