zoukankan      html  css  js  c++  java
  • C#反射生成简单sql语句

     static void Main(string[] args)
            {
                book book = new book();//实体类
                booktest b1 = new booktest();
               
                book.bookid = "1";
                book.bookname = "计算机原理";
                book.bookprice = 32.04M;
                string sql = CreateInsertSQL(book);
            }
    
    
            public static string CreateInsertSQL(book book)
            {
                Type type = book.GetType();
                
                PropertyInfo[] props = type.GetProperties();
                StringBuilder sb = new StringBuilder();
                sb.Append("insert into "+ type.Name+"(");
                foreach( PropertyInfo prop in props)
                {
                    string name = prop.PropertyType.FullName;
                   string value  = prop.GetValue(book,null) as string;
                    object[] array = prop.GetCustomAttributes(typeof(KEYAttribute),true);//获取属性,判断在sql语句中必须的,比如主键
                    if (array.Length > 0)
                    {
                        continue;
                    }
                    sb.Append(prop.Name + ",");
    
                }
                sb.Remove(sb.Length - 1, 1);
                sb.Append(") values(");
                foreach (PropertyInfo prop in props)
                {
                    object[] array = prop.GetCustomAttributes(typeof(KEYAttribute), true);
                    if (array.Length > 0)
                    {
                        continue;
                    }
                    sb.Append("@" + prop.Name + ",");
                }
            
                sb.Remove(sb.Length-1,1);
                sb.Append(")");
                return sb.ToString();
            }
    View Code
  • 相关阅读:
    bzoj4033
    bzoj 1197
    bzoj 1196
    bzoj 1195
    bzoj 1194
    bzoj 1193
    bzoj 1192
    jvm系列(一):java类的加载机制
    红黑树之 原理和算法详细介绍
    TreeMap详细介绍(源码解析)和使用示例
  • 原文地址:https://www.cnblogs.com/zxiong/p/4106171.html
Copyright © 2011-2022 走看看