⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ormremover.cs

📁 一个小型的ORM框架,写得不好请多多指教
💻 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.BAK
//{
//    /// <summary>
//    /// Orm删除器,负责删除指定的Model对应的数据库记录
//    /// </summary>
//    public class OrmRemover
//    {
//        /// <summary>
//        /// 删除指定的单据,如果有子单据存在那么先删除子单据
//        /// </summary>
//        /// <param name="ModelObject">记录Model</param>
//        /// <param name="id">记录ID</param>
//        /// <returns>True - 删除成功,false - 删除错误</returns>
//        public bool Remove(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;

//            using (TransactionScope scope = new TransactionScope())
//            {
//                //先判断此单据是否存在子单据,如果存在那么先删除子单据,删除子单句之前必须先读取所有此单据信息,然后做删除
//                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(type, id);
//                        }
//                    }
//                }

//                //删除本单据
//                using (SqlConnection conn = new SqlConnection(INS.DBUtility.SqlHelper.INSClient_Trade_ConnectionString))
//                {
//                    conn.Open();
//                    SqlCommand cmd = new SqlCommand();
//                    cmd.Connection = conn;
//                    cmd.CommandType = CommandType.Text;
//                    cmd.Parameters.Add(new SqlParameter("@" + strKeyName, id));
//                    cmd.CommandText = string.Format(strDeleteSQL, new object[] { strTablename, strKeyName + " = @" + strKeyName });

//                    //删除
//                    cmd.ExecuteNonQuery();
//                }

//                //提交事务
//                scope.Complete();
//            }
//            return true;
//        }

//        /// <summary>
//        /// 删除子单据
//        /// </summary>
//        /// <param name="SubObjectType">要删除的子单据类型</param>
//        /// <param name="foreignkey">子单据的外键</param>
//        private void RemoveSubObject(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;

//            using (TransactionScope scope = new TransactionScope())
//            {
//                //先判断此单据是否存在子单据,如果存在那么先删除子单据,删除子单句之前必须先读取所有此单据信息,然后做删除
//                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);

//                            using (SqlConnection conn = new SqlConnection(INS.DBUtility.SqlHelper.INSClient_Trade_ConnectionString))
//                            {
//                                conn.Open();
//                                SqlCommand cmd = new SqlCommand();
//                                cmd.Connection = conn;
//                                cmd.CommandType = CommandType.Text;
//                                cmd.Parameters.Add(new SqlParameter("@" + strForeignKey, ForeignKeyValue));
//                                cmd.CommandText = string.Format(strSeleteSQL, new object[] { strKeyName, strTablename, strForeignKey + " = @" + strForeignKey });

//                                using (SqlDataReader rd = cmd.ExecuteReader(CommandBehavior.CloseConnection))
//                                {
//                                    while (rd.Read())
//                                    {
//                                        //如果有子表存在,那么删除之
//                                        RemoveSubObject(type, rd[0]);
//                                    }
//                                }
//                            } // --using (SqlConnection conn ...
//                        }
//                    }  // --if (CustomerAttributes.Length > 0)
//                }

//                //删除本单据
//                using (SqlConnection conn = new SqlConnection(INS.DBUtility.SqlHelper.INSClient_Trade_ConnectionString))
//                {
//                    conn.Open();
//                    SqlCommand cmd = new SqlCommand();
//                    cmd.Connection = conn;
//                    cmd.CommandType = CommandType.Text;
//                    cmd.Parameters.Add(new SqlParameter("@" + strForeignKey, ForeignKeyValue));
//                    cmd.CommandText = string.Format(strDeleteSQL, new object[] { strTablename, strForeignKey + " = @" + strForeignKey });

//                    //删除
//                    cmd.ExecuteNonQuery();
//                }

//                //提交事务
//                scope.Complete();
//            }
//        }
//    }
//}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -