码迷,mamicode.com
首页 > 数据库 > 详细

根据实例类型反射操作数据库(简单通用表操作类)

时间:2016-08-03 13:25:28      阅读:213      评论:0      收藏:0      [点我收藏+]

标签:

这个类适用简单的表

1.有且只有id为主键,

2.并且实例类主键,也就是id应为字段,其他为属性

3.实例类名跟表名一样,字段属性跟列名一样

public class ProType
    {
        public int id;
       
        
        public string type
        {
            get;
            set;
        }
        public int array
        {
            get;
            set;
        }
        public string relyTpye
        {
            get;
            set;
        }
        public string etype
        {
            get;
            set;
        }
    }

  下面就是该类的实现

    
/// <summary>
        /// 排序枚举
        /// </summary>

public enum OrderType { asc,desc} public class SqlDAL<T> where T:new () { private PropertyInfo[] propertys; private List<string> nameList; public SqlDAL() { T t = new T(); nameList = new List<string>(); propertys = t.GetType().GetProperties(); //MessageBox.Show(propertys.Length.ToString()); foreach (var item in propertys) { nameList.Add(item.Name); } } /// <summary> /// 查询 /// </summary> /// <param name="orderType">排序</param> /// <param name="str">排序字段</param> /// <returns></returns> public List<T> Select(OrderType orderType,params string[] str) { string orderStr = ""; if (str.Length > 0) { orderStr += " order by " + str[0]; for (int i = 1; i < str.Length; i++) { orderStr += "," + str[i]; } orderStr += " " + orderType.ToString(); } T t = new T(); string sql = "select * from " + t.GetType().Name + orderStr; DataTable dt = DBHelper.GetInfo(sql); return (List<T>)ModelConvertHelper<T>.ConvertToModel(dt); } /// <summary> ////// </summary> /// <param name="t"></param> /// <returns></returns> public bool Insert(T t) { List<SqlParameter> parm = new List<SqlParameter>(); foreach (var item in propertys) { SqlParameter parmitem = new SqlParameter(item.Name,item.GetValue(t,null)); parm.Add(parmitem); } string strName = ""; string strValue = ""; strName += nameList[0]; strValue += "@" + nameList[0]; for (int i = 1; i < nameList.Count; i++) { strName += "," + nameList[i]; strValue += ",@" + nameList[i]; } string sql = "insert into "+t.GetType().Name.ToString() +" ("+strName+") value ("+strValue+")"; int isAdd = DBHelper.Commed(sql,parm.ToArray()); if (isAdd > 0) { return true; } else { return false; } } /// <summary> ////// </summary> /// <param name="t"></param> /// <returns></returns> public bool Update(T t) { List<SqlParameter> parm = new List<SqlParameter>(); foreach (var item in propertys) { SqlParameter parmitem = new SqlParameter(item.Name,item.GetValue(t,null)); parm.Add(parmitem); } string str = ""; str = nameList[0] + "=@" + nameList[0]; for (int i = 1; i < nameList.Count; i++) { str += "," + nameList[i] + "=@" + nameList[i]; } string sql = "update "+t.GetType().Name+" set "+str+" where id=@id"; int isUpdate = DBHelper.Commed(sql,parm.ToArray()); if (isUpdate > 0) { return true; } else { return false; } } /// <summary> ////// </summary> /// <param name="id"></param> /// <returns></returns> public bool Delete(string id) { T t = new T(); string sql = "delete from" + t.GetType().Name + " where id=" + id; int isDelete = DBHelper.Commed(sql); if (isDelete > 0) { return true; }else { return false; } } }

这里之所以有这个类,是因为操作这些表的代码大同小异,甚至可以做出通用任意表,实例类可以用特征类来标识,做出字段与列名的相对应

但那种太过繁琐,用来实现这些简单表就有点大材小用,所有先弄出了这个类,如果后面有机会再实现以下任意表通用类

根据实例类型反射操作数据库(简单通用表操作类)

标签:

原文地址:http://www.cnblogs.com/lsgsanxiao/p/5732339.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!