zoukankan      html  css  js  c++  java
  • 利用反射动态构成sql语句

    class Program
        {
            static void Main(string[] args)
            {
                People p = new People();
                Insert(p);
            }

            public static bool Insert(object obj)
            {
                Type type = obj.GetType();
                string tableName = "tb_" + type.Name;
                string sql = "insert into " + tableName + "(";
                PropertyInfo[] properties = type.GetProperties();
                foreach (PropertyInfo pInfo in properties)
                {
                    sql += pInfo.Name + ",";
                }
                sql = sql.Substring(0, sql.LastIndexOf(','));
                sql += ") values(";
                foreach (PropertyInfo pInfo in properties)
                {
                    sql += "'" + pInfo.GetValue(obj, null) + "',";
                }
                sql = sql.Substring(0, sql.LastIndexOf(','));
                sql += ")";

                return true;
            }   
        }
        class People
        {
            public string Name { set; get; }
            public string Age { set; get; }
            public string Sex { set; get; }
        }

  • 相关阅读:
    外观模式及php实现
    桥接模式与装饰者模式的区别
    装饰者模式及php实现
    组合模式和php实现
    桥接模式和php实现
    适配器模式和php实现
    建造者模式以及php实现
    原型模式及php实现
    单例模式及php实现
    抽象工厂模式和php实现
  • 原文地址:https://www.cnblogs.com/aukle/p/3226026.html
Copyright © 2011-2022 走看看