|
|
|
@ -16,12 +16,15 @@ namespace OMS.NET.DbClass
|
|
|
|
Console.WriteLine("===========this is a test area=================");
|
|
|
|
Console.WriteLine("===========this is a test area=================");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
|
|
/// 将对象本身插入到数据库中
|
|
|
|
|
|
|
|
/// </summary>
|
|
|
|
public virtual void Insert()
|
|
|
|
public virtual void Insert()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
var type = GetType();
|
|
|
|
var type = GetType();
|
|
|
|
var insertSql = SqlHelper.GetInsertSql(GetType());
|
|
|
|
var insertSql = SqlHelper.GetInsertSql(GetType());
|
|
|
|
using MySqlConnection connection = new(GlobalArea.ConnectionStringWithDbName);
|
|
|
|
using MySqlConnection connection = new(GlobalArea.ConnectionStringWithDbName);
|
|
|
|
connection.Open(); // 确保在执行命令之前打开连接
|
|
|
|
connection.Open(); //确保在执行命令之前打开连接
|
|
|
|
using var command = new MySqlCommand(insertSql, connection);
|
|
|
|
using var command = new MySqlCommand(insertSql, connection);
|
|
|
|
AddParameters(command);
|
|
|
|
AddParameters(command);
|
|
|
|
Console.WriteLine($"SQL: {command.CommandText}");
|
|
|
|
Console.WriteLine($"SQL: {command.CommandText}");
|
|
|
|
@ -38,7 +41,7 @@ namespace OMS.NET.DbClass
|
|
|
|
var type = GetType();
|
|
|
|
var type = GetType();
|
|
|
|
var updateSql = SqlHelper.GetUpdateSql(GetType());
|
|
|
|
var updateSql = SqlHelper.GetUpdateSql(GetType());
|
|
|
|
using MySqlConnection connection = new(GlobalArea.ConnectionStringWithDbName);
|
|
|
|
using MySqlConnection connection = new(GlobalArea.ConnectionStringWithDbName);
|
|
|
|
connection.Open(); // 确保在执行命令之前打开连接
|
|
|
|
connection.Open();
|
|
|
|
using var command = new MySqlCommand(updateSql, connection);
|
|
|
|
using var command = new MySqlCommand(updateSql, connection);
|
|
|
|
AddParameters(command);
|
|
|
|
AddParameters(command);
|
|
|
|
command.ExecuteNonQuery();
|
|
|
|
command.ExecuteNonQuery();
|
|
|
|
@ -47,25 +50,29 @@ namespace OMS.NET.DbClass
|
|
|
|
|
|
|
|
|
|
|
|
public virtual void Delete()
|
|
|
|
public virtual void Delete()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
var type = GetType();
|
|
|
|
|
|
|
|
var deleteSql = SqlHelper.GetDeleteSql(GetType());
|
|
|
|
var deleteSql = SqlHelper.GetDeleteSql(GetType());
|
|
|
|
using MySqlConnection connection = new(GlobalArea.ConnectionStringWithDbName);
|
|
|
|
using MySqlConnection connection = new(GlobalArea.ConnectionStringWithDbName);
|
|
|
|
connection.Open(); // 确保在执行命令之前打开连接
|
|
|
|
connection.Open();
|
|
|
|
using var command = new MySqlCommand(deleteSql, connection);
|
|
|
|
using var command = new MySqlCommand(deleteSql, connection);
|
|
|
|
AddParameters(command);
|
|
|
|
AddParameters(command);
|
|
|
|
command.ExecuteNonQuery();
|
|
|
|
command.ExecuteNonQuery();
|
|
|
|
Log.Info($"{SqlHelper.GetTableName(type)}数据表删除({string.Join(',', ShowDetail())})");
|
|
|
|
Log.Info($"{SqlHelper.GetTableName(GetType())}数据表删除({string.Join(',', ShowDetail())})");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void AddParameters(MySqlCommand command)
|
|
|
|
private void AddParameters(MySqlCommand command)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
|
|
|
|
//bool IsIdentity = SqlHelper.IsIdentity(GetType());
|
|
|
|
foreach (var property in SqlHelper.GetProperties(GetType()))
|
|
|
|
foreach (var property in SqlHelper.GetProperties(GetType()))
|
|
|
|
{
|
|
|
|
{
|
|
|
|
|
|
|
|
if (property.GetCustomAttribute<ColumnAttribute>()!.IsIdentity)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
}
|
|
|
|
command.Parameters.AddWithValue("@" + property.Name, property.GetValue(this));
|
|
|
|
command.Parameters.AddWithValue("@" + property.Name, property.GetValue(this));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private IEnumerable<string> ShowDetail()
|
|
|
|
public IEnumerable<string> ShowDetail()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
return SqlHelper.GetProperties(GetType()).Select(p => $"{p.Name}={p.GetValue(this)}");
|
|
|
|
return SqlHelper.GetProperties(GetType()).Select(p => $"{p.Name}={p.GetValue(this)}");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|