ORM工具
http://www.cnblogs.com/zhoutk/archive/2013/01/09/2852160.html
最近在看反射,突然想写一个ORM工具,要轻量级的,不要配置文档,先不管效率,就是一个小工具,在项目初期方便挂数据库。
我的目标就是在数据库中建个表,在项目中写个模型,然后用上这个ORM工具,就能实现数据库的基本增删改查。
有想法就动手做,翠花上代码:
增
public bool Insert( object entity) { Type t = entity.GetType(); PropertyInfo[] properties = t.GetProperties(); if (properties.Count<PropertyInfo>() > 1 ) // 至少两个字段,一个主键,一个数据字段。 { string sql = " Insert into " + t.Name; // 约定:类名即为数据表名。 string fieldList = "" ; string fieldVals = "" ; foreach (PropertyInfo field in properties) { if (field.Name.CompareTo( " Id " ) == 0 ) // 约定:字段名若为Id,必为主键,且自动增长。 continue ; switch (field.PropertyType.Name) // 约定:属性名即为数据字段名。 { case " String " : case " DateTime " : fieldList += field.Name + " , " ; fieldVals += " ' " + field.GetValue(entity, null ) + " ', " ; break ; case " Int32 " : case " Double " : fieldList += field.Name + " , " ; fieldVals += field.GetValue(entity, null ) + " , " ; break ; case " Boolean " : fieldList += field.Name + " , " ; fieldVals += (( bool )field.GetValue(entity, null ) ? 1 : 0 ) + " , " ; break ; default : break ; } } fieldList = fieldList.Remove(fieldList.Length - 1 ); fieldVals = fieldVals.Remove(fieldVals.Length - 1 ); sql += " ( " + fieldList + " ) values ( " + fieldVals + " ) " ; return db.ExecuteSql(sql); } else // "出错,没有字段被组合!" return false ; }
改
public bool Modify( object entity) { Type t = entity.GetType(); PropertyInfo[] properties = t.GetProperties(); if (properties.Count<PropertyInfo>() > 1 ) // 至少两个字段,一个主键,一个数据字段。 { string sql = " Update " + t.Name + " set " ; // 约定:类名即为数据表名。 string fieldSet = "" ; string fieldCondition = "" ; bool firstEntry = true ; foreach (PropertyInfo field in properties) { if (firstEntry) // 约定:第一个字段为主键。 { if (field.PropertyType.Name.StartsWith( " Int " )) fieldCondition += field.Name + " = " + field.GetValue(entity, null ); else fieldCondition += field.Name + " =' " + field.GetValue(entity, null ) + " ' " ; firstEntry = false ; } else switch (field.PropertyType.Name) // 约定:属性名即为数据字段名。 { case " String " : case " DateTime " : fieldSet += field.Name + " =' " + field.GetValue(entity, null ) + " ', " ; break ; case " Int32 " : case " Double " : fieldSet += field.Name + " = " + field.GetValue(entity, null ) + " , " ; break ; case " Boolean " : fieldSet += field.Name + " = " + (( bool )field.GetValue(entity, null ) ? 1 : 0 ) + " , " ; break ; default : break ; } } fieldSet = fieldSet.Remove(fieldSet.Length - 1 ); sql += fieldSet + " where " + fieldCondition; return db.ExecuteSql(sql); } else // "出错,没有字段被组合!" return false ; }
删
public bool Delete( object entity) { Type t = entity.GetType(); PropertyInfo[] properties = t.GetProperties(); if (properties.Count<PropertyInfo>() > 0 ) { string sql = " Delete From " + t.Name + " where " ; string fieldCondition = "" ; if (properties[ 0 ].PropertyType.Name.StartsWith( " Int " )) fieldCondition += properties[ 0 ].Name + " = " + properties[ 0 ].GetValue(entity, null ); else fieldCondition += properties[ 0 ].Name + " =' " + properties[ 0 ].GetValue(entity, null ) + " ' " ; sql += fieldCondition; return db.ExecuteSql(sql); } else return false ; }
查
public bool Select( object entity) { Type t = entity.GetType(); PropertyInfo[] properties = t.GetProperties(); if (properties.Count<PropertyInfo>() > 1 ) // 至少两个字段,一个主键,一个数据字段。 { string sql = " Select " ; // 约定:类名即为数据表名。 string fieldList = "" ; string fieldCondition = "" ; bool firstEntry = true ; foreach (PropertyInfo field in properties) { if (firstEntry) // 约定:第一个字段为主键。 { if (field.PropertyType.Name.StartsWith( " Int " )) fieldCondition += field.Name + " = " + field.GetValue(entity, null ); else fieldCondition += field.Name + " =' " + field.GetValue(entity, null ) + " ' " ; firstEntry = false ; } else switch (field.PropertyType.Name) // 约定:属性名即为数据字段名。 { case " String " : case " DateTime " : fieldList += field.Name + " , " ; break ; case " Int32 " : case " Double " : fieldList += field.Name + " , " ; break ; case " Boolean " : fieldList += field.Name + " , " ; break ; default : break ; } } fieldList = fieldList.Remove(fieldList.Length - 1 ); sql += fieldList + " from " + t.Name + " where " + fieldCondition; var rs = db.getResult(sql); if (rs.Count > 0 ) { int index = 1 ; foreach ( var al in rs) { switch (properties[index].PropertyType.Name) // 约定:属性名即为数据字段名。 { case " String " : properties[index].SetValue(entity, al, null ); break ; case " DateTime " : properties[index].SetValue(entity, DateTime.Parse(al.ToString()), null ); break ; case " Int32 " : properties[index].SetValue(entity, Int32.Parse(al.ToString()), null ); break ; case " Double " : properties[index].SetValue(entity, Double.Parse(al.ToString()), null ); break ; case " Boolean " : properties[index].SetValue(entity, Boolean.Parse(al.ToString()), null ); break ; default : break ; } index ++ ; } return true ; } else return false ; } else // "出错,没有字段被组合!" return false ; }
上面的代码实现了最基本的单条纪录的增删改查,其中最麻烦的是查,最简单的是删。
其中的db变量是数据库操作对象,这个大家都熟悉,我就不贴代码了,上面的代码提供思路供大家参考,若哪位兄弟需要代码,请到下面链接去下载,保证能正常运行,例子中给的是MYSQL的操作方法。
下载链接: http://download.csdn.net/detail/ztk12/4975759
MySql.Data.dll 这个动态链接库被我从项目中移除了,大家如果机子上没有的话,自己去下一个吧。
分类: C# , 算法
标签: C# ORM 反射
作者: Leo_wl
出处: http://www.cnblogs.com/Leo_wl/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
版权信息声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://www.haodehen.cn/did47268