diff --git a/Src/Asp.NetCore2/SqlSugar/Enum/DbType.cs b/Src/Asp.NetCore2/SqlSugar/Enum/DbType.cs index 143ac64fa..1bfbd2715 100644 --- a/Src/Asp.NetCore2/SqlSugar/Enum/DbType.cs +++ b/Src/Asp.NetCore2/SqlSugar/Enum/DbType.cs @@ -15,6 +15,7 @@ namespace SqlSugar Dm, Kdbndp, Oscar, + [Obsolete("使用DbType.MySql,已经全部统一用MySqlConnector取代 MySql.Data 原因.NET7下面差了几倍性能")] MySqlConnector, Access, OpenGauss, diff --git a/Src/Asp.NetCore2/SqlSugar/OnlyCore/DataExtensions.cs b/Src/Asp.NetCore2/SqlSugar/OnlyCore/DataExtensions.cs index 88e6a223e..c92f3e46b 100644 --- a/Src/Asp.NetCore2/SqlSugar/OnlyCore/DataExtensions.cs +++ b/Src/Asp.NetCore2/SqlSugar/OnlyCore/DataExtensions.cs @@ -1,7 +1,7 @@ using Dm; using Kdbndp; using Microsoft.Data.Sqlite; -using MySql.Data.MySqlClient; +using MySqlConnector; using Npgsql; using Oracle.ManagedDataAccess.Client; using System; diff --git a/Src/Asp.NetCore2/SqlSugar/Realization/MySql/MySqlProvider.cs b/Src/Asp.NetCore2/SqlSugar/Realization/MySql/MySqlProvider.cs index 88ac5d651..4214d01af 100644 --- a/Src/Asp.NetCore2/SqlSugar/Realization/MySql/MySqlProvider.cs +++ b/Src/Asp.NetCore2/SqlSugar/Realization/MySql/MySqlProvider.cs @@ -1,4 +1,4 @@ -using MySql.Data.MySqlClient; +using MySqlConnector; using System; using System.Collections.Generic; using System.Data; @@ -148,5 +148,174 @@ namespace SqlSugar Check.ExceptionEasy($"To upgrade the MySql.Data. Error:{ex.Message}", $" 请先升级MySql.Data 。 详细错误:{ex.Message}"); } } + + #region async + public async Task CloseAsync() + { + if (this.Transaction != null) + { + this.Transaction = null; + } + if (this.Connection != null && this.Connection.State == ConnectionState.Open) + { + await (this.Connection as MySqlConnection).CloseAsync(); + } + if (this.IsMasterSlaveSeparation && this.SlaveConnections.HasValue()) + { + foreach (var slaveConnection in this.SlaveConnections) + { + if (slaveConnection != null && slaveConnection.State == ConnectionState.Open) + { + await (slaveConnection as MySqlConnection).CloseAsync(); + } + } + } + } + public async Task GetCommandAsync(string sql, SugarParameter[] parameters) + { + MySqlCommand sqlCommand = new MySqlCommand(sql, (MySqlConnection)this.Connection); + sqlCommand.CommandType = this.CommandType; + sqlCommand.CommandTimeout = this.CommandTimeOut; + if (this.Transaction != null) + { + sqlCommand.Transaction = (MySqlTransaction)this.Transaction; + } + if (parameters.HasValue()) + { + IDataParameter[] ipars = ToIDbDataParameter(parameters); + sqlCommand.Parameters.AddRange((MySqlParameter[])ipars); + } + if (this.Connection.State != ConnectionState.Open) + { + try + { + await (this.Connection as MySqlConnection).OpenAsync(); + } + catch (Exception ex) + { + Check.Exception(true, ex.Message); + } + } + return sqlCommand; + } + public override async Task ExecuteCommandAsync(string sql, params SugarParameter[] parameters) + { + try + { + Async(); + InitParameters(ref sql, parameters); + if (FormatSql != null) + sql = FormatSql(sql); + SetConnectionStart(sql); + if (this.ProcessingEventStartingSQL != null) + ExecuteProcessingSQL(ref sql, ref parameters); + ExecuteBefore(sql, parameters); + var sqlCommand = await GetCommandAsync(sql, parameters); + int count; + if (this.CancellationToken == null) + count = await sqlCommand.ExecuteNonQueryAsync(); + else + count = await sqlCommand.ExecuteNonQueryAsync(this.CancellationToken.Value); + if (this.IsClearParameters) + sqlCommand.Parameters.Clear(); + ExecuteAfter(sql, parameters); + sqlCommand.Dispose(); + return count; + } + catch (Exception ex) + { + CommandType = CommandType.Text; + if (ErrorEvent != null) + ExecuteErrorEvent(sql, parameters, ex); + throw ex; + } + finally + { + if (this.IsAutoClose()) + { + await this.CloseAsync(); + } + SetConnectionEnd(sql); + } + } + public override async Task GetDataReaderAsync(string sql, params SugarParameter[] parameters) + { + try + { + Async(); + InitParameters(ref sql, parameters); + if (FormatSql != null) + sql = FormatSql(sql); + SetConnectionStart(sql); + var isSp = this.CommandType == CommandType.StoredProcedure; + if (this.ProcessingEventStartingSQL != null) + ExecuteProcessingSQL(ref sql, ref parameters); + ExecuteBefore(sql, parameters); + var sqlCommand = await GetCommandAsync(sql, parameters); + DbDataReader sqlDataReader; + if (this.CancellationToken == null) + sqlDataReader = await sqlCommand.ExecuteReaderAsync(this.IsAutoClose() ? CommandBehavior.CloseConnection : CommandBehavior.Default); + else + sqlDataReader = await sqlCommand.ExecuteReaderAsync(this.IsAutoClose() ? CommandBehavior.CloseConnection : CommandBehavior.Default, this.CancellationToken.Value); + if (isSp) + DataReaderParameters = sqlCommand.Parameters; + if (this.IsClearParameters) + sqlCommand.Parameters.Clear(); + ExecuteAfter(sql, parameters); + SetConnectionEnd(sql); + if (SugarCompatible.IsFramework || this.Context.CurrentConnectionConfig.DbType != DbType.Sqlite) + sqlCommand.Dispose(); + return sqlDataReader; + } + catch (Exception ex) + { + CommandType = CommandType.Text; + if (ErrorEvent != null) + ExecuteErrorEvent(sql, parameters, ex); + throw ex; + } + } + public override async Task GetScalarAsync(string sql, params SugarParameter[] parameters) + { + try + { + Async(); + InitParameters(ref sql, parameters); + if (FormatSql != null) + sql = FormatSql(sql); + SetConnectionStart(sql); + if (this.ProcessingEventStartingSQL != null) + ExecuteProcessingSQL(ref sql, ref parameters); + ExecuteBefore(sql, parameters); + var sqlCommand = await GetCommandAsync(sql, parameters); + object scalar; + if (CancellationToken == null) + scalar = await sqlCommand.ExecuteScalarAsync(); + else + scalar = await sqlCommand.ExecuteScalarAsync(this.CancellationToken.Value); + //scalar = (scalar == null ? 0 : scalar); + if (this.IsClearParameters) + sqlCommand.Parameters.Clear(); + ExecuteAfter(sql, parameters); + sqlCommand.Dispose(); + return scalar; + } + catch (Exception ex) + { + CommandType = CommandType.Text; + if (ErrorEvent != null) + ExecuteErrorEvent(sql, parameters, ex); + throw ex; + } + finally + { + if (this.IsAutoClose()) + { + await this.CloseAsync(); + } + SetConnectionEnd(sql); + } + } + #endregion } } diff --git a/Src/Asp.NetCore2/SqlSugar/Realization/MySql/SqlBuilder/MySqlBlukCopy.cs b/Src/Asp.NetCore2/SqlSugar/Realization/MySql/SqlBuilder/MySqlBlukCopy.cs index 25bcf559a..2d565ff30 100644 --- a/Src/Asp.NetCore2/SqlSugar/Realization/MySql/SqlBuilder/MySqlBlukCopy.cs +++ b/Src/Asp.NetCore2/SqlSugar/Realization/MySql/SqlBuilder/MySqlBlukCopy.cs @@ -1,4 +1,4 @@ -using MySql.Data.MySqlClient; +using MySqlConnector; using System; using System.Collections.Generic; using System.Data; diff --git a/Src/Asp.NetCore2/SqlSugar/Realization/MySql/SqlBuilder/MySqlFastBuilder.cs b/Src/Asp.NetCore2/SqlSugar/Realization/MySql/SqlBuilder/MySqlFastBuilder.cs index 5f6a11153..ef722825a 100644 --- a/Src/Asp.NetCore2/SqlSugar/Realization/MySql/SqlBuilder/MySqlFastBuilder.cs +++ b/Src/Asp.NetCore2/SqlSugar/Realization/MySql/SqlBuilder/MySqlFastBuilder.cs @@ -1,4 +1,4 @@ -using MySql.Data.MySqlClient; +using MySqlConnector; using System; using System.Collections.Generic; using System.Data; @@ -56,18 +56,22 @@ namespace SqlSugar File.Delete(fileName); } } - catch (MySqlException ex) + catch (Exception ex) { if (ex.Message == "The used command is not allowed with this MySQL version") { Check.ExceptionEasy("connection string add : AllowLoadLocalInfile=true", "BulkCopy MySql连接字符串需要添加 AllowLoadLocalInfile=true; 添加后如果还不行Mysql数据库执行一下 SET GLOBAL local_infile=1 "); } + else if (ex.Message.Contains("To use MySqlBulkLoader.Local=true, set Allo")) + { + Check.ExceptionEasy("connection string add : AllowLoadLocalInfile=true", "BulkCopy MySql连接字符串需要添加 AllowLoadLocalInfile=true; 添加后如果还不行Mysql数据库执行一下 SET GLOBAL local_infile=1 "); + } else if (ex.Message == "Loading local data is disabled; this must be enabled on both the client and server sides") { this.Context.Ado.ExecuteCommand("SET GLOBAL local_infile=1"); Check.ExceptionEasy(ex.Message, " 检测到你没有开启文件,已自动执行 SET GLOBAL local_infile=1 在试一次"); } - else + else { throw; } diff --git a/Src/Asp.NetCore2/SqlSugar/SqlSugar.csproj b/Src/Asp.NetCore2/SqlSugar/SqlSugar.csproj index 8c0b2d983..f9bfd6712 100644 --- a/Src/Asp.NetCore2/SqlSugar/SqlSugar.csproj +++ b/Src/Asp.NetCore2/SqlSugar/SqlSugar.csproj @@ -21,7 +21,7 @@ - + diff --git a/Src/Asp.NetCore2/SqlSugar/SqlSugarForCore.nuspec b/Src/Asp.NetCore2/SqlSugar/SqlSugarForCore.nuspec index e0c7f8202..9559a1baa 100644 --- a/Src/Asp.NetCore2/SqlSugar/SqlSugarForCore.nuspec +++ b/Src/Asp.NetCore2/SqlSugar/SqlSugarForCore.nuspec @@ -19,7 +19,7 @@ - +