首页 > 代码库 > C#反射生成简单sql语句
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 = http://www.mamicode.com/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(); }
C#反射生成简单sql语句
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。