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; }
        }

  • 相关阅读:
    gitee ssh key
    Visual Studio Code自定义快捷键(eclipse习惯)
    Maven settings.xml
    Android Studio
    windows压缩图片
    maven生成项目慢解决办法
    区块链学习专栏
    windows常用目录
    windows常用命令
    jQuery列表选择美化插件uichoose
  • 原文地址:https://www.cnblogs.com/aukle/p/3226026.html
Copyright © 2011-2022 走看看