using Cysharp.Threading.Tasks;
using MySql.Data.MySqlClient;
namespace MySQLTool
{
public static class MySQLHelper
{
public static string connectionString = $"SERVER=server;DATABASE=database;UID=uid;PASSWORD=password;";
#region 同步操作
///
/// 同步执行查询操作
///
/// sql 语句
/// 参数
/// 字典数据
public static List> ExecuteQuery(string query, Dictionary parameters = null)
{
List> result = new List>();
using (MySqlConnection connection = new MySqlConnection(connectionString))
{
connection.Open();
MySqlCommand command = new MySqlCommand(query, connection);
if (parameters != null)
{
foreach (var parameter in parameters)
{
command.Parameters.AddWithValue(parameter.Key, parameter.Value);
}
}
MySqlDataReader dataReader = command.ExecuteReader();
while (dataReader.Read())
{
var row = new Dictionary();
for (int i = 0; i < dataReader.FieldCount; i++)
{
row.Add(dataReader.GetName(i), dataReader[i].ToString());
}
result.Add(row);
}
dataReader.Close();
}
return result;
}
///
/// 同步执行一条 sql 语句
///
/// sql 语句
/// 参数
/// 操作条数
public static int ExecuteNonQuery(string query, Dictionary parameters = null)
{
using (MySqlConnection connection = new MySqlConnection(connectionString))
{
connection.Open();
MySqlCommand command = new MySqlCommand(query, connection);
if (parameters != null)
{
foreach (var parameter in parameters)
{
command.Parameters.AddWithValue(parameter.Key, parameter.Value);
}
}
return command.ExecuteNonQuery();
}
}
///
/// 同步查询
///
/// 表名
/// 参数
/// 数据类型
/// 数据列表
public static List Query(string talbeName, Dictionary parameters = null) where T : new()
{
List result = new List();
using (MySqlConnection connection = new MySqlConnection(connectionString))
{
connection.Open();
MySqlCommand command = new MySqlCommand($"SELECT * FROM {talbeName}", connection);
if (parameters != null)
{
foreach (var parameter in parameters)
{
command.Parameters.AddWithValue(parameter.Key, parameter.Value);
}
}
using (MySqlDataReader dataReader = command.ExecuteReader())
{
while (dataReader.Read())
{
T item = new T();
for (int i = 0; i < dataReader.FieldCount; i++)
{
var propertyName = dataReader.GetName(i);
var propertyInfo = typeof(T).GetProperty(propertyName);
if (propertyInfo != null && dataReader[propertyName] != DBNull.Value)
{
object value = Convert.ChangeType(dataReader[propertyName], propertyInfo.PropertyType);
propertyInfo.SetValue(item, value, null);
}
}
result.Add(item);
}
}
}
return result;
}
///
/// 同步插入数据
///
/// 表名
/// 数据
/// 数据类型
/// 操作条数
public static int Insert(string table, T data)
{
using (MySqlConnection connection = new MySqlConnection(connectionString))
{
connection.Open();
var properties = typeof(T).GetProperties();
var columns = string.Join(", ", properties.Select(p => p.Name));
var values = string.Join(", ", properties.Select(p => $"@{p.Name}"));
MySqlCommand command = connection.CreateCommand();
command.CommandText = $"INSERT INTO {table} ({columns}) VALUES ({values})";
foreach (var property in properties)
{
var parameter = command.CreateParameter();
parameter.ParameterName = $"@{property.Name}";
parameter.Value = property.GetValue(data);
command.Parameters.Add(parameter);
}
return command.ExecuteNonQuery();
}
}
///
/// 同步更新数据
///
/// 表名
/// 数据
/// 条件
/// 数据类型
/// 操作条数
public static int Update(string table, T data, string condition)
{
using (MySqlConnection connection = new MySqlConnection(connectionString))
{
connection.Open();
var properties = typeof(T).GetProperties();
var updateColumns = string.Join(", ", properties.Select(p => $"{p.Name} = @{p.Name}"));
MySqlCommand command = connection.CreateCommand();
command.CommandText = $"UPDATE {table} SET {updateColumns} WHERE {condition}";
foreach (var property in properties)
{
var parameter = command.CreateParameter();
parameter.ParameterName = $"@{property.Name}";
parameter.Value = property.GetValue(data);
command.Parameters.Add(parameter);
}
return command.ExecuteNonQuery();
}
}
///
/// 同步删除数据
///
/// 表明
/// 条件
/// 操作条数
public static int Delete(string table, string condition)
{
string query = $"DELETE FROM {table} WHERE {condition}";
using (MySqlConnection connection = new MySqlConnection(connectionString))
{
connection.Open();
MySqlCommand command = new MySqlCommand(query, connection);
return command.ExecuteNonQuery();
}
}
#endregion
#region 异步操作
///
/// 异步查询
///
/// sql 语句
/// 参数
/// 数据字典
public static async UniTask>> ExecuteQueryAsync(string query, Dictionary parameters = null)
{
List> result = new List>();
using (MySqlConnection connection = new MySqlConnection(connectionString))
{
await connection.OpenAsync();
MySqlCommand command = new MySqlCommand(query, connection);
if (parameters != null)
{
foreach (var parameter in parameters)
{
command.Parameters.AddWithValue(parameter.Key, parameter.Value);
}
}
MySqlDataReader dataReader = command.ExecuteReader();
while (await dataReader.ReadAsync())
{
var row = new Dictionary();
for (int i = 0; i < dataReader.FieldCount; i++)
{
row.Add(dataReader.GetName(i), dataReader[i].ToString());
}
result.Add(row);
}
dataReader.Close();
}
return result;
}
///
/// 异步执行一条语句
///
/// sql 语句
/// 参数
/// 操作条数
public static async UniTask ExecuteNonQueryAsync(string query, Dictionary parameters = null)
{
using (MySqlConnection connection = new MySqlConnection(connectionString))
{
await connection.OpenAsync();
MySqlCommand command = new MySqlCommand(query, connection);
if (parameters != null)
{
foreach (var parameter in parameters)
{
command.Parameters.AddWithValue(parameter.Key, parameter.Value);
}
}
return await command.ExecuteNonQueryAsync();
}
}
///
/// 异步查询数据
///
/// 表明
/// 参数
/// 数据类型
/// 查询到的列表
public static async UniTask> QueryAsync(string talbeName, Dictionary parameters = null) where T : new()
{
List result = new List();
using (MySqlConnection connection = new MySqlConnection(connectionString))
{
await connection.OpenAsync();
MySqlCommand command = new MySqlCommand($"SELECT * FROM {talbeName}", connection);
if (parameters != null)
{
foreach (var parameter in parameters)
{
command.Parameters.AddWithValue(parameter.Key, parameter.Value);
}
}
using (MySqlDataReader dataReader = command.ExecuteReader())
{
while (await dataReader.ReadAsync())
{
T item = new T();
for (int i = 0; i < dataReader.FieldCount; i++)
{
var propertyName = dataReader.GetName(i);
var propertyInfo = typeof(T).GetProperty(propertyName);
if (propertyInfo != null && dataReader[propertyName] != DBNull.Value)
{
object value = Convert.ChangeType(dataReader[propertyName], propertyInfo.PropertyType);
propertyInfo.SetValue(item, value, null);
}
}
result.Add(item);
}
}
}
return result;
}
///
/// 异步插入一条数据
///
/// 表名
/// 数据
/// 数据类型
/// 操作条数
public static async UniTask InsertAsync(string table, T data)
{
using (MySqlConnection connection = new MySqlConnection(connectionString))
{
await connection.OpenAsync();
var properties = typeof(T).GetProperties();
var columns = string.Join(", ", properties.Select(p => p.Name));
var values = string.Join(", ", properties.Select(p => $"@{p.Name}"));
MySqlCommand command = connection.CreateCommand();
command.CommandText = $"INSERT INTO {table} ({columns}) VALUES ({values})";
foreach (var property in properties)
{
var parameter = command.CreateParameter();
parameter.ParameterName = $"@{property.Name}";
parameter.Value = property.GetValue(data);
command.Parameters.Add(parameter);
}
return await command.ExecuteNonQueryAsync();
}
}
///
/// 异步更新数据
///
/// 表名
/// 数据
/// 条件
/// 数据类型
/// 操作条数
public static async UniTask UpdateAsync(string table, T data, string condition)
{
using (MySqlConnection connection = new MySqlConnection(connectionString))
{
await connection.OpenAsync();
var properties = typeof(T).GetProperties();
var updateColumns = string.Join(", ", properties.Select(p => $"{p.Name} = @{p.Name}"));
MySqlCommand command = connection.CreateCommand();
command.CommandText = $"UPDATE {table} SET {updateColumns} WHERE {condition}";
foreach (var property in properties)
{
var parameter = command.CreateParameter();
parameter.ParameterName = $"@{property.Name}";
parameter.Value = property.GetValue(data);
command.Parameters.Add(parameter);
}
return await command.ExecuteNonQueryAsync();
}
}
///
/// 异步删除数据
///
/// 表名
/// 条件
/// 操作条数
public static async UniTask DeleteAsync(string table, string condition)
{
using (MySqlConnection connection = new MySqlConnection(connectionString))
{
await connection.OpenAsync();
string query = $"DELETE FROM {table} WHERE {condition}";
MySqlCommand command = new MySqlCommand(query, connection);
return await command.ExecuteNonQueryAsync();
}
}
#endregion
}
}