首页 > 解决方案 > 使用反射和 Ado.net 将多个对象动态插入数据库

问题描述

我试图解决它很长时间。任何帮助将不胜感激。基本上,我需要动态地将多个对象插入数据库。该方法应适用于 3 或 3+ 级关系。Like - 该方法将能够插入包含房间对象列表的 House 对象,而房间对象包含家具对象列表。每个对象都将以递归过程进入数据库。这意味着如果找到通用列表,该方法将调用自身。

class Program
    {
        static void Main(string[] args)
        {
            var house1 = new House()
            {
                Id = 1,
                HouseName = "Hasan's villa",
                HouseAddress = "Riyadh",
                Rooms = new List<Room>() { new Room() { Id = 01, Rent = 1200,

                    Furnitures = new List<Furniture>() { new Furniture() { Id = 001, FurnitureName = "Table", Price = 45 },
                                                       { new Furniture() { Id = 002, FurnitureName = "Bookshelf", Price = 55 } } } },

                    new Room(){Id = 02, Rent = 1500, Furnitures = new List<Furniture> {new Furniture(){Id = 003 , FurnitureName = "Almari", Price = 75}} }

                }
            };

var connectionString = "Server=SQLEXPRESS//FERTILE-FIEL;Database=ASPNETB4;Trusted_Connection=True;";
            var dataOperation = new DataOperation<House>(connectionString);

            dataOperation.Insert(house1);
    }


class DataOperation<T> : IDataOperation<T> where T : Entity
    {
        private readonly string _connectionString;
        private SqlConnection connection;

        public DataOperation(string connectionString)
        {
            _connectionString = connectionString;
        }
        public void Insert(T item)
        {
            EntityMaker entityMaker = new EntityMaker();
            entityMaker.InsertEntity(item);
        }
    }



class EntityMaker
    {
        public object genericProp { get; set; }
        public void InsertEntity(object entity)
        {
            Type type = entity.GetType();
            string name = type.Name;

    

    var propertyNames = new ArrayList();
    
            var propertyValue = new ArrayList();

//Need to make the sql dynamic. I have just hard coded it for now.

string sql = "INSERT INTO " + name + " (" + propertyNames[0]+',' + propertyNames[1]+','+ propertyNames [3]+") 
    VALUES(" + propertyValues[0]+',' + propertyValues[1]+',' + propertyValues[2]+");";

        var properties = type.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly);

        foreach (var prop in properties)
        {

            if (prop.PropertyType.IsGenericType)
            {
                genericProp = prop.PropertyType;
                Type genType = (Type)genericProp;
                var types = genType.GetGenericArguments()[0];

                var genName = types.Name;

                InsertEntity(genName);
            }
            else
            {
                var propName = prop.Name;
                var propValue = prop.GetValue(entity).ToString();

                if (propName != null)
                {
                    propertyNames.Add(propName).ToString();
                    propertyValue.Add(propValue).ToString();
                }
            }

        }

感谢您考虑问题。

标签: c#genericsrecursionreflectioninsert

解决方案


推荐阅读