这个仅是为了培训做的一个小例子

public class DB     {

public static string GetClassName(Type type)

{

if (type == null)

throw new ArgumentException("参数type不能为空");

else{

if (type.HasAttribute<DBTableNameAttribute>())

return type.GetAttribute<DBTableNameAttribute>().Name;

else

return type.Name;

  }

}

public static T AddObject<T>(T o) where T :class

{

string tablename = GetClassName(typeof(T));

string fieldName;

object fieldValue;

string[] fieldNames;

object[] fieldValues;

System.Reflection.PropertyInfo[] Props = typeof(T).GetProperties();

fieldNames=new string[Props.Length ];

fieldValues =new object [Props.Length ];

string checkstr = "";

for (int i = 0; i < Props.Length; i++)

{

fieldName = Props[i].Name;

fieldValue = Props[i].GetValue(o, null);

if (fieldValue == null)                     continue;

fieldValue = CommonFunction.SafeSQL(fieldValue.ToString());

fieldNames[i] = fieldName;

fieldValues[i] = fieldValue;

object[] attributes = Props[i].GetCustomAttributes(false);

foreach (Attribute a in attributes)

{

//判断Attribute 中是否 为 UniqueColumnAttribute

if (a is UniqueColumnAttribute)

{

checkstr = "not exists(select * from " +tablename + " where " + fieldName  + "='" + fieldValue + "')";

}

}

}

if (checkstr != "")

{

checkstr = "if("+checkstr +")";

}

string insert = "" ,tail="";

insert = "insert into " + tablename + " (";

tail = "values(";

for (int i = 0; i < Props.Length; i++)

{

fieldName = fieldNames[i] ;

if (fieldName.ToUpper() == "ID") continue;

object  _fieldValue = fieldValues[i];

if (_fieldValue == null)

{ }

else

      {

insert += fieldName + ",";

tail += "'" + _fieldValue + "',";

}

}

insert = insert.Remove(insert.Length - 1);

tail = tail.Remove(tail.Length - 1);

string insertstr = insert + ")" + " " + tail + ")";

if (checkstr != "")

insertstr = checkstr +"begin "+ insertstr+" end";

insertstr+=" select @@identity"

SqlHelper link = new SqlHelper();

int r=  link.UpdateDataBase(insertstr);

if(r>0)     {

o.Id=r;

return o;

}

return null;

}

public static bool  DelObject<T>( int id) where T : class

{

if (id == 0) return false;

string tablename = GetClassName(typeof(T));

string insert = "";

insert =string.Format( "delete from {0} where id={1}",tablename,id)

SqlHelper link = new SqlHelper();

int r = link.UpdateDataBase(insert);

return true;

}

public static bool  DelObject<T>(T o) where T : class

{

string tablename = GetClassName(typeof(T));

string fieldName;

object fieldValue;

string[] fieldNames;

object[] fieldValues;

System.Reflection.PropertyInfo[] Props = typeof(T).GetProperties();

fieldNames = new string[Props.Length];

fieldValues = new object[Props.Length];

for (int i = 0; i < Props.Length; i++)

{

fieldName = Props[i].Name;

fieldValue = Props[i].GetValue(o, null);

fieldNames[i] = fieldName;

fieldValues[i] = fieldValue;

}

string str="";

for (int i = 0; i < Props.Length; i++)

{

fieldName = fieldNames[i];

string _fieldValue = fieldValues[i].ToString();

if (fieldName.ToUpper() == "ID")

{

str = "delete from " + tablename + " where id=" + _fieldValue ;

break;

}

}

if(!string.IsNullOrEmpty(str))

{

SqlHelper link = new SqlHelper();

int r = link.UpdateDataBase(str);

return true;

}

return false;

}

public static bool UpdateObject<T>(T o) where T : class

{

string tablename = GetClassName(typeof(T));

string fieldName;

object fieldValue;

string[] fieldNames;

object[] fieldValues;

System.Reflection.PropertyInfo[] Props = typeof(T).GetProperties();

fieldNames = new string[Props.Length];

fieldValues = new object[Props.Length];

for (int i = 0; i < Props.Length; i++)

{

if (Props[i].DeclaringType.IsValueType)

{

fieldName = Props[i].Name;

fieldValue = Props[i].GetValue(o, null);

fieldNames[i] = fieldName;

fieldValues[i] = fieldValue;

}

else

{

fieldNames[i] = null;

fieldValues[i] = null;

}

}

string str = "update "+tablename +" set ",where=" where id=";

int id = 0;

for (int i = 0; i < Props.Length; i++)

{

fieldName = fieldNames[i];

object  _fieldValue = fieldValues[i] ;

if (fieldName == null) continue;

if (fieldName.ToUpper() == "ID")

{

if (_fieldValue == null)

{

}

else

{

int.TryParse(_fieldValue.ToString(), out id);

//str = "delete from " + tablename + " where id=" + _fieldValue;

where += id.ToString();

}

//break;

}

else

{

if (_fieldValue == null)

{

}

else

str += fieldName +  "='" + _fieldValue.ToString() + "',";

}

}

if (id <= 0) return false;

str = str.Remove(str.Length - 1);

str += where;

SqlHelper link = new SqlHelper();

int r = link.UpdateDataBase(str);

return true;

}

public static T SelObject<T>(int id) where T : class

{

if (id <= 0) return null ;

string tablename = GetClassName(typeof(T));

string insert = "";

insert = "select * from " + tablename + " where id=" + id.ToString();

SqlHelper link = new SqlHelper();

DataTable dt = link.SelectDataBase(insert);

if (dt == null || dt.Rows.Count == 0) return null;

Type type = typeof(T);

Object obj = type.Assembly.CreateInstance(type.FullName);

System.Reflection.PropertyInfo[] Props = typeof(T).GetProperties();

for (int i = 0; i < Props.Length; i++)

{

Props[i].SetValue(obj, dt.Rows[0][Props[i].Name], null);

}

return (T)obj;

}

public static T SelObject<T>(BaseQuery query  ) where T : class

{

if (query==null )

return null;

string tablename = GetClassName(typeof(T));

if (tablename.EndsWith("Query"))

tablename = tablename.Replace("Query","");

string fieldName;

object fieldValue;

string[] fieldNames;

object[] fieldValues;

System.Reflection.PropertyInfo[] Props = query.GetType().GetProperties();

fieldNames = new string[Props.Length];

fieldValues = new object[Props.Length];

for (int i = 0; i < Props.Length; i++)

{

fieldName = Props[i].Name;

fieldValue = Props[i].GetValue(query, null);

fieldNames[i] = fieldName;

fieldValues[i] = fieldValue;

}

string insert = "",where="1=1";

insert = "select * from " + tablename + " where "  ;

int id = 0;

for (int i = 0; i < Props.Length; i++)

{

fieldName = fieldNames[i];

object _fieldValue = fieldValues[i];

if (fieldName.ToUpper() == "ID")

{

if (_fieldValue == null)

{ }

else

{

int.TryParse(_fieldValue.ToString(), out id);

//str = "delete from " + tablename + " where id=" + _fieldValue;

where +="Id="+ id.ToString();

}

//break;

}

else

{

if (_fieldValue == null)

{ }

else

{

if (string.IsNullOrEmpty(_fieldValue.ToString()))

{ }

else

where += " and " + fieldName + "='" + _fieldValue.ToString() + "'";

}

}

}

insert = insert + where;

SqlHelper link = new SqlHelper();

DataTable dt = link.SelectDataBase(insert);

if (dt == null || dt.Rows.Count == 0) return null;

Type type = typeof(T);

Object obj = type.Assembly.CreateInstance(type.FullName );

System.Reflection.PropertyInfo[] Props2 = typeof(T).GetProperties();

for (int i = 0; i < Props2.Length; i++)

{

Props2[i].SetValue(obj, dt.Rows[0][Props2[i].Name], null);

}

return (T)obj;

}

public static T SelObject<T>(string where) where T : class

{

if (string.IsNullOrEmpty(where)) )                 return null;

string tablename = GetClassName(typeof(T));

string insert = "";

insert = "select * from " + tablename + " where " + where;

SqlHelper link = new SqlHelper();

DataTable dt = link.SelectDataBase(insert);

if (dt == null || dt.Rows.Count == 0) return null;

Type type = typeof(T);

Object obj = type.Assembly.CreateInstance(type.FullName);

System.Reflection.PropertyInfo[] Props = typeof(T).GetProperties();

for (int i = 0; i < Props.Length; i++)

{

Props[i].SetValue(obj, dt.Rows[0][Props[i].Name], null);

}

return (T)obj;

}

public static List<T> ListObjects<T>(string where) where T : class

{

try

{

if (string.IsNullOrEmpty(where))

where = "1=1";

string tablename = GetClassName(typeof(T));

string insert = "";

insert = "select * from " + tablename + " where " + where;

SqlHelper link = new SqlHelper();

DataTable dt = link.SelectDataBase(insert);

if (dt == null || dt.Rows.Count == 0) return null;

Type type = typeof(T);

List<T> list = new List<T>();

for (int row = 0; row < dt.Rows.Count; row++)

{

Object obj = type.Assembly.CreateInstance(type.FullName );

System.Reflection.PropertyInfo[] Props = typeof(T).GetProperties();

for (int i = 0; i < Props.Length; i++)

{

Props[i].SetValue(obj, dt.Rows[row][Props[i].Name], null);

}

list.Add((T)obj);

}

return list;

}

catch

{

return null;

}

}

}

原创最简单的ORM例子的更多相关文章

  1. D2010 RTTI + Attribute 简单实现ORM

    还记得David I 今年四月来盛大时,被问及“反射机制能再做得好一点吗?我们想放弃RTTI”,David I 回答“这的确是需要考虑的地方,当然RTTI我们不会放弃的”.(这个白胡子的老哥哥还真很可 ...

  2. 使用Multiplayer Networking做一个简单的多人游戏例子-2/3(Unity3D开发之二十六)

    猴子原创,欢迎转载.转载请注明: 转载自Cocos2Der-CSDN,谢谢! 原文地址: http://blog.csdn.net/cocos2der/article/details/51007512 ...

  3. 使用Multiplayer Networking做一个简单的多人游戏例子-1/3(Unity3D开发之二十五)

    猴子原创,欢迎转载.转载请注明: 转载自Cocos2Der-CSDN,谢谢! 原文地址: http://blog.csdn.net/cocos2der/article/details/51006463 ...

  4. 一个简单的CORBA例子

    因为对CORBA分析的需要,这里写一个简单的CORBA例子.从JDK1.2开始,JDK中集成了ORB的实现,本例子使用了JDK1.7,对于JDK1.2+应该都没有问题.这个例子实现一个简单的加减乘除的 ...

  5. 菜鸟学习Hibernate——简单的一个例子

    一.Hibernate开发. 上篇博客已经为大家介绍了持久层框架的发展流程,持久层框架的种类. 为了能够使用Hibernate快速上手,我们先讲解一个简单的Hibernate应用实例hibernate ...

  6. 一个简单的ORM制作(SQL帮助类)

    一个简单的ORM制作大概需要以下几个类: SQL执行类 CURD操作类 其他酱油类 先从SQL执行类说起,可能会涉及数据库的迁移等问题,所以需要定义一个接口以方便迁移到其他数据库, 事务没提供命名,若 ...

  7. 轻松创建nodejs服务器(1):一个简单nodejs服务器例子

    这篇文章主要介绍了一个简单nodejs服务器例子,本文实现了一个简单的hello world例子,并展示如何运行这个服务器,需要的朋友可以参考下   我们先来实现一个简单的例子,hello world ...

  8. C#基础笔记---浅谈XML读取以及简单的ORM实现

    背景: 在开发ASP.NETMVC4 项目中,虽然web.config配置满足了大部分需求,不过对于某些特定业务,我们有时候需要添加新的配置文件来记录配置信息,那么XML文件配置无疑是我们选择的一个方 ...

  9. 使用Multiplayer Networking做一个简单的多人游戏例子-3/3(Unity3D开发之二十七)

    使用Multiplayer Networking做一个简单的多人游戏例子-1/3 使用Multiplayer Networking做一个简单的多人游戏例子-2/3 使用Multiplayer Netw ...

随机推荐

  1. linux svn搭建

    1 安装: yum install subversion 2 查看svn安装信息: rpm -ql subversion 3 创建svn根目录: svnserve -d -r /svn 4 进入/sv ...

  2. android开发------编写用户界面之线性布局

    一个好的应用程序离不开人性化的用户界面.在学习其他东西之前.理应先学习编写程序的布局(外观) 今天,我们就来学习android的UI布局----LinearLayout. LinearLayout,即 ...

  3. Activity has leaked window that was originally added -界面退出时未关闭对话框异常 android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running? -

    退出Activity时弹出登录框,点击确定finish当前Activity,结果报了这个错,随后查找资料知道 原因: 是因为退出Activity时没有关闭弹出框,出现了这个错误 解决方法: 只需要在a ...

  4. 关于php插件pdo_mysql的安装

    今天在做一个商城的连接的时候,需要MpDO验证.需要安装pdo_mysql模块,刚开始按照php扩展模块的安装按照这个安装ZIP,curl都成功了但是安装pdo_mysql却不行,在./configu ...

  5. JS简单加密

    //简单的jS加密解密//code为对应的字符串,h为(2,8,10,16)就是要转成的几进制function en(code, h) { var monyer = new Array();var i ...

  6. poj3177 && poj3352 边双连通分量缩点

    Redundant Paths Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12676   Accepted: 5368 ...

  7. poj2631 树的直径

    设s-t是这棵树的直径,那么对于任意给予的一点,它能够到达的最远的点是s或者t. 这样我们可以通过2次bfs找到树的直径了. #include<cstdio> #include<qu ...

  8. Web前端性能优化教程06:减少DNS查找、避免重定向

    本文是Web前端性能优化系列文章中的第六篇,主要讲述内容:减少DNS查找.避免重定向.完整教程可查看:  一.减少DNS查找 基础知识 DNS(Domain Name System): 负责将域名UR ...

  9. Struts中Action三种接收参数的方式?

    前言: 前面已经有一篇随笔介绍了Struts2的大概原理.本文就Struts2中Action与jsp页面进行数据对接时介绍几种常见方法! 值栈ValueStack 3个Action Action1 p ...

  10. Python 中文编码问题小结

    1. 下面的语句要放在代码开头,指定文件编码, 可以识别 脚本中的所有字符和中文. # -*- coding:utf-8 -*- 2. codecd 编码转换 如果想要读取文本中的中文,需要借助于co ...