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


    这个类适用简单的表

    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>
            /// <returns></returns>
            public DataTable Select()
            {
                T t = new T();
                string sql = "select * from " + t.GetType().Name;
                DataTable dt = new DataTable();
                dt = DBHelper.GetInfo(sql);
                return dt;
            }
            /// <summary>
            /// 查询
            /// </summary>
            /// <param name="orderType">排序</param>
            /// <param name="str">排序字段</param>
            /// <returns></returns>
            public DataTable 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 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 + ") values (" + 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;
                }
    
            }
        }

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

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

  • 相关阅读:
    BZOJ2870 最长道路
    为什么要设置Java环境变量
    JavaMail收发邮件的步骤
    Java 7开发者预览版发布
    J2EE的13种核心技术
    jdk1.5、1.6、1.7新特性详细介绍(整理)
    JAVAEE5 VS JAVAEE6
    TOCMAT的web.xml详解(转贴)
    setAttribute()和getAttribute()
    jsp servlet的区别和联系
  • 原文地址:https://www.cnblogs.com/lsgsanxiao/p/5732339.html
Copyright © 2020-2023  润新知