反射特性

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Reflection;
using Hotel.Modal.Attributes;
using Hotel.DAL;
using System.Data;

namespace Hotel.BLL
{
public enum SQLHeadMod
{
///


/// 添加
///

Insert,
///
/// 删除
///

Delete,
///
/// 修改
///

Update

}
public class ORMHepler
{

///
/// 增。删。改的方法
///

/// 对象
/// 对象信息
/// 查询条件
///
public static int SearchInfo(Object Entity,SQLHeadMod sqltype,string IsIndentityId)
{
int count =0;
string idcount =null;
string Identity = null ;
Type type = Entity.GetType();//获取类的原型信息
//1.获取表名
string TableName = GetTableName(type);
//获得Sql语句的成分 “select s.*,g.* from Subject as s inner join Grade as g where .......”
StringBuilder lieming = new StringBuilder();//列明
StringBuilder canshu = new StringBuilder(); //参数
List canshuzhi = new List();//参数值

foreach(PropertyInfo proitem in type.GetProperties())
{
if (Identity == null)
{
idcount = Id(proitem);
Identity = idcount;
}
//判断该字段是否是自动编号且该字段存在于表
if(!IsIndentityNo(proitem) && IsColum(proitem) )
{
string name = GetColumName(proitem);
lieming.Append(name +" = "+ "@" + name + ",");
canshu.Append("@" + name + ",");
canshuzhi.Add(new SqlParameter("@"+ name, proitem.GetValue(Entity, null)));
}
}
//截取追后一个逗号
lieming.Remove(lieming.Length-1,1);
canshu.Remove(canshu.Length-1,1);
//拼接Sql语句
if(!string.IsNullOrEmpty(Entity.ToString()))
{
string idusql = null;
switch(sqltype)
{
case SQLHeadMod.Insert :
idusql = "insert [" + TableName + "] " + " select " + canshu.ToString();
count = SqlHelper.ExecuteNonQuery(idusql, canshuzhi.ToArray());
break;
case SQLHeadMod.Delete :
idusql = "delete [" + TableName + "] " + " where " + idcount +"=" + IsInd

entityId;
count = SqlHelper.ExecuteNonQuery(idusql);
break;
case SQLHeadMod.Update :
idusql = "update [" + TableName + "] " + " set " + lieming.ToString() + " where " + idcount + "=" + IsIndentityId;
count = SqlHelper.ExecuteNonQuery(idusql,canshuzhi.ToArray());
break;
}
}
return count;
}
///


/// 主键列名
///

///
private static string Id(PropertyInfo idid)
{
string idcount = null;
if (IsPrimaryKey(idid))
{
object[] obj = idid.GetCustomAttributes(typeof(Property), false);
if (obj == null || obj.Length == 0)
return null;
Property id = obj[0] as Property;
if (id != null)
{
idcount = id.Column ;
}
}
return idcount;
}
///
/// 判断是否主键
///

///
private static Boolean IsPrimaryKey(PropertyInfo pro)
{
object[] obj = pro.GetCustomAttributes(typeof(PrimaryKey), false);
if (obj.Length == 0 || obj == null)
return false;//不是自动编号
PrimaryKey id = obj[0] as PrimaryKey;
if (id == null)
{
return false;//不是自动编号
}
return id.IsPrimarykey;//是自动编号
}
///
/// 获取该列字段名
///

///
private static string GetColumName(PropertyInfo proinfo)
{
string ColumName = https://www.360docs.net/doc/a413352701.html,;
object[] obj = proinfo.GetCustomAttributes(typeof(Property), false);
Property colum = obj[0] as Property;
if (colum != null && !string.IsNullOrEmpty(colum.Column ))
{
ColumName = colum.Column;
}
return ColumName;
}
///
/// 判断该列是否存在
///

///
///
private static Boolean IsColum(PropertyInfo proinfo)
{
object[] obj = proinfo.GetCustomAttributes(typeof(Property),false);
return obj != null && obj.Length > 0;
}
///
/// 判断是否自动编号
///

///
private static Boolean IsIndentityNo(PropertyInfo proinfo)
{
object[] obj = proinfo.GetCustomAttributes(typeof(Id),false);
if(obj.Length == 0 || obj == null)

return false;//不是自动编号
Id id = obj[0] as Id;
if(id == null)
{
return false;//不是自动编号
}
return id.IsIdentityNO ;//是自动编号
}
///


/// 获取表名
///

///
private static string GetTableName(Type Entity)
{
//类名与表名一致时,表名等于类名(https://www.360docs.net/doc/a413352701.html,)
string TableName = https://www.360docs.net/doc/a413352701.html,;
//表明与类名不一致时

//得到特性上的属性
object[] obj = Entity.GetCustomAttributes(typeof(Entity),false);
if(obj != null || obj.Length >0 )//特性个数不为零
{
if(!string.IsNullOrEmpty((obj[0] as Entity).TableName))//表名不为空
{
TableName = (obj[0] as Entity).TableName;
}
}
return TableName;
}

///
/// 查询方法
///

///
///
///
public static List GetList(string sql,params SqlParameter [] ps)
{
DataTable tb = SqlHelper.ExecuteQuery(sql,ps);
return FindConvertToList(tb);
}

///
/// 封装成list
///

///
///
///
private static List FindConvertToList(DataTable table)
{
List list = new List();
foreach(DataRow row in table.Rows)
{
list.Add(ConvertToEntity(row));
}
return list;
}
///
/// 把DataRow转化成T对象
///

///
///
private static T ConvertToEntity(DataRow row)
{
Type type = typeof(T);
//构造类的实例
T t = Activator.CreateInstance();
foreach(PropertyInfo proitem in type.GetProperties())
{
proitem.SetValue(t, row[GetColumName(proitem)], null);
}
return t;
}
}
}

相关文档
最新文档