Remoe mysql.data

This commit is contained in:
sunkaixuan
2022-12-10 15:12:34 +08:00
parent 529583c41d
commit 7bbec50c69
7 changed files with 182 additions and 8 deletions

View File

@@ -15,6 +15,7 @@ namespace SqlSugar
Dm,
Kdbndp,
Oscar,
[Obsolete("使用DbType.MySql已经全部统一用MySqlConnector取代 MySql.Data 原因.NET7下面差了几倍性能")]
MySqlConnector,
Access,
OpenGauss,

View File

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

View File

@@ -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<DbCommand> 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<int> 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<IDataReader> 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<object> 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
}
}

View File

@@ -1,4 +1,4 @@
using MySql.Data.MySqlClient;
using MySqlConnector;
using System;
using System.Collections.Generic;
using System.Data;

View File

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

View File

@@ -21,7 +21,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.Data.Sqlite" Version="5.0.5" />
<PackageReference Include="MySql.Data" Version="8.0.31" />
<PackageReference Include="MySqlConnector" Version="2.2.2" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.2" />
<PackageReference Include="Npgsql" Version="7.0.0" />
<PackageReference Include="Oracle.ManagedDataAccess.Core" Version="3.21.1" />

View File

@@ -19,7 +19,7 @@
<dependency id="Newtonsoft.Json" version="13.0.2" />
<dependency id="Microsoft.Data.Sqlite" version="5.0.5" />
<dependency id="System.Reflection.Emit.Lightweight" version="4.3.0" />
<dependency id="MySql.Data" version="8.0.31" />
<dependency id="MySqlConnector" version="2.2.2" />
<dependency id="Oracle.ManagedDataAccess.Core" version="3.21.1" />
<dependency id="Npgsql" version="7.0.0" />
<dependency id="SqlSugarCore.Dm" version="1.0.0" />