📄 ormremover.cs
字号:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Reflection;
using System.Transactions;
namespace MiniORM
{
/// <summary>
/// Orm删除器,负责删除指定的Model对应的数据库记录
/// </summary>
public class OrmRemover
{
public bool Remove(object ModelObject, int id)
{
using (SqlConnection conn = new SqlConnection(PubFuncs.ConnectionStr))
{
conn.Open();
SqlTransaction trans = conn.BeginTransaction();
bool rtn = Remove(trans, ModelObject, id);
trans.Commit();
return rtn;
}
}
/// <summary>
/// 删除指定的单据,如果有子单据存在那么先删除子单据
/// </summary>
/// <param name="ModelObject">记录Model</param>
/// <param name="id">记录ID</param>
/// <returns>True - 删除成功,false - 删除错误</returns>
public bool Remove(SqlTransaction trans, object ModelObject, int id)
{
//删除操作,首先应该先删除子单据表,再删除主表
string strTablename = PubFuncs.GetTableName(ModelObject.GetType());
string strKeyName = PubFuncs.GetKey(ModelObject.GetType());
string strDeleteSQL = "DELETE FROM {0} WHERE {1}";
MiniORMAttribute.SubDataObjectAttribute SubDataAttr = null;
PropertyInfo[] props = ModelObject.GetType().GetProperties();
object[] CustomerAttributes;
//先判断此单据是否存在子单据,如果存在那么先删除子单据,删除子单句之前必须先读取所有此单据信息,然后做删除
foreach (PropertyInfo prop in props)
{
CustomerAttributes = prop.GetCustomAttributes(typeof(MiniORMAttribute.SubDataObjectAttribute), false);
if (CustomerAttributes.Length > 0)
{
SubDataAttr = CustomerAttributes[0] as MiniORMAttribute.SubDataObjectAttribute;
if (SubDataAttr != null)
{
Type type = PubFuncs.GetObjectType(SubDataAttr.AssemblyName, SubDataAttr.NamespaceName, SubDataAttr.ClassName);
//如果有子表存在,那么删除之
RemoveSubObject(trans, type, id);
}
}
}
SqlCommand cmd = new SqlCommand();
cmd.Connection = trans.Connection;
cmd.Transaction = trans;
cmd.CommandType = CommandType.Text;
cmd.Parameters.Add(new SqlParameter("@" + strKeyName, id));
cmd.CommandText = string.Format(strDeleteSQL, new object[] { strTablename, strKeyName + " = @" + strKeyName });
//删除
cmd.ExecuteNonQuery();
return true;
}
/// <summary>
/// 删除子单据
/// </summary>
/// <param name="SubObjectType">要删除的子单据类型</param>
/// <param name="foreignkey">子单据的外键</param>
private void RemoveSubObject(SqlTransaction trans, Type SubObjectType, object ForeignKeyValue)
{
string strTablename = PubFuncs.GetTableName(SubObjectType);
string strKeyName = PubFuncs.GetKey(SubObjectType);
string strForeignKey = PubFuncs.GetForeignKey(SubObjectType);
string strSeleteSQL = "SELECT {0} FROM {1} WHERE {2}";
string strDeleteSQL = "DELETE FROM {0} WHERE {1}";
MiniORMAttribute.SubDataObjectAttribute SubDataAttr = null;
PropertyInfo[] props = SubObjectType.GetProperties();
object[] CustomerAttributes;
//先判断此单据是否存在子单据,如果存在那么先删除子单据,删除子单句之前必须先读取所有此单据信息,然后做删除
foreach (PropertyInfo prop in props)
{
CustomerAttributes = prop.GetCustomAttributes(typeof(MiniORMAttribute.SubDataObjectAttribute), false);
if (CustomerAttributes.Length > 0)
{
SubDataAttr = CustomerAttributes[0] as MiniORMAttribute.SubDataObjectAttribute;
if (SubDataAttr != null)
{
Type type = PubFuncs.GetObjectType(SubDataAttr.AssemblyName, SubDataAttr.NamespaceName, SubDataAttr.ClassName);
SqlCommand cmd1 = new SqlCommand();
cmd1.Connection = trans.Connection;
cmd1.CommandType = CommandType.Text;
cmd1.Parameters.Add(new SqlParameter("@" + strForeignKey, ForeignKeyValue));
cmd1.CommandText = string.Format(strSeleteSQL, new object[] { strKeyName, strTablename, strForeignKey + " = @" + strForeignKey });
using (SqlDataReader rd = cmd1.ExecuteReader())
{
while (rd.Read())
{
//如果有子表存在,那么删除之
RemoveSubObject(trans, type, rd[0]);
}
}
}
} // --if (CustomerAttributes.Length > 0)
}
SqlCommand cmd = new SqlCommand();
cmd.Connection = trans.Connection; ;
cmd.Transaction = trans;
cmd.CommandType = CommandType.Text;
cmd.Parameters.Add(new SqlParameter("@" + strForeignKey, ForeignKeyValue));
cmd.CommandText = string.Format(strDeleteSQL, new object[] { strTablename, strForeignKey + " = @" + strForeignKey });
//删除
cmd.ExecuteNonQuery();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -