• 利用反射动态构成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; }
        }

  • 相关阅读:
    C++中const变量使用注意
    const对象调用static成员函数
    Error in startup script: value for " " missing
    check tcl version
    socket
    grid pack
    mouse bind
    gesture
    pixel
    open
  • 原文地址:https://www.cnblogs.com/aukle/p/3226026.html
Copyright © 2020-2023  润新知