示例#1
0
 /// <summary>
 /// 封装数据表数据到实体集合的方法
 /// </summary>
 /// <param name="cmdType">命令类型</param>
 /// <param name="cmdText">要执行的SQL语句或存储过程名称</param>
 /// <param name="values">SQL语句或存储过程的参数列表</param>
 /// <returns>返回实体类的集合</returns>
 protected List <T> GetBySql(CommandType cmdType, string cmdText, SqlParameter[] values)
 {
     using (SqlDataReader reader = DBHelper.GetReader(DBHelper.CONSTR, cmdType, cmdText, values))
     {
         List <T>       lst                   = new List <T>();
         Type           entityType            = typeof(T);
         PropertyInfo[] properties            = entityType.GetProperties();
         Dictionary <string, XmlClassMap> dic = EntityMapperHandler.GetInstance().GetMapDictionary();
         XmlClassMap classMap                 = dic[entityType.Name];
         while (reader.Read())
         {
             T entity = (T)entityType.Assembly.CreateInstance(entityType.FullName);
             foreach (PropertyInfo property in properties)
             {
                 if (property.CanWrite && classMap.Properties.ContainsKey(property.Name))
                 {
                     object value = reader[classMap.Properties[property.Name].ColumnName];
                     if (value != null && value != DBNull.Value)
                     {
                         property.SetValue(entity, value, null);
                     }
                 }
             }
             lst.Add(entity);
         }
         reader.Close();
         return(lst);
     }
 }
示例#2
0
        private void CreateMapperDictionary(Stream mapperStream)
        {
            XmlDocument doc = new XmlDocument();

            doc.Load(mapperStream);
            //解析实体类与表的映射
            XmlNodeList nl = doc.GetElementsByTagName("class");

            foreach (XmlNode node in nl)
            {
                string className = node.Attributes["name"].Value;
                string tableName = node.Attributes["table"].Value;

                //解析属性与字段的映射
                XmlClassMap classMap = new XmlClassMap(className, tableName);
                XmlNodeList childNl  = node.ChildNodes;
                foreach (XmlNode childNode in childNl)
                {
                    if (childNode.Name == "property")
                    {
                        #region 解析属性

                        string propertyName = childNode.Attributes["name"].Value;
                        string columnName   = childNode.Attributes["column"].Value;

                        XmlPropertyMap propertyMap = new XmlPropertyMap(propertyName, columnName);
                        classMap.Properties.Add(propertyName, propertyMap);

                        #endregion
                        #region 解析自增列

                        XmlAttribute attrIdentity = childNode.Attributes["isIdentity"];
                        if (attrIdentity != null && attrIdentity.Value == "true")
                        {
                            classMap.Identity = propertyMap;
                        }

                        #endregion
                        #region 解析主键

                        XmlAttribute attrPK = childNode.Attributes["isPK"];
                        if (attrPK != null && attrPK.Value == "true")
                        {
                            classMap.Ids.Add(propertyName, propertyMap);
                        }

                        #endregion
                    }
                }
                mapperDictionary.Add(className, classMap);
            }
            mapperStream.Close();
            mapperStream.Dispose();
        }
示例#3
0
 /// <summary>
 /// 分页查询方法,基于分页存储过程
 /// </summary>
 /// <param name="pageResult">用于传递查询条件的分页类的对象</param>
 /// <returns>返回封装了页面数据和总记录数据的分页类对象</returns>
 public PageResult <T> GetPageData(PageResult <T> pageResult)
 {
     SqlParameter[] values =
     {
         new SqlParameter("TableName",    pageResult.TableName),
         new SqlParameter("ReturnFields", pageResult.ReturnFields),
         new SqlParameter("PageSize",     pageResult.PageSize),
         new SqlParameter("PageIndex",    pageResult.PageIndex),
         new SqlParameter("Where",        pageResult.Where),
         new SqlParameter("Orderfld",     pageResult.Orderfld),
         new SqlParameter("OrderType",    pageResult.OrderType)
     };
     using (SqlDataReader reader = DBHelper.GetReader(DBHelper.CONSTR, CommandType.StoredProcedure, this.procedureName, values))
     {
         List <T>       lst                   = new List <T>();
         Type           entityType            = typeof(T);
         PropertyInfo[] properties            = entityType.GetProperties();
         Dictionary <string, XmlClassMap> dic = EntityMapperHandler.GetInstance().GetMapDictionary();
         XmlClassMap classMap                 = dic[entityType.Name];
         //提取当前页的数据
         while (reader.Read())
         {
             T entity = (T)entityType.Assembly.CreateInstance(entityType.FullName);
             foreach (PropertyInfo property in properties)
             {
                 if (property.CanWrite && classMap.Properties.ContainsKey(property.Name))
                 {
                     object value = reader[classMap.Properties[property.Name].ColumnName];
                     if (value != null && value != DBNull.Value)
                     {
                         property.SetValue(entity, value, null);
                     }
                 }
             }
             lst.Add(entity);
         }
         pageResult.Data = lst;
         //提取总记录数
         if (reader.NextResult())
         {
             while (reader.Read())
             {
                 pageResult.RecordCount = Convert.ToInt32(reader["RecordCount"]);
                 break;
             }
         }
         reader.Close();
     }
     return(pageResult);
 }
示例#4
0
        private string procedureName = "MesnacPaging";                                                  //分页存储过程名

        #endregion

        #region 构造方法

        public BaseService()
        {
            //获取实体类T的映射信息
            this.classMap = EntityMapperHandler.GetInstance().GetMapDictionary()[typeof(T).Name];
            //获取实体类的属性信息
            PropertyInfo[] pis = typeof(T).GetProperties();
            foreach (PropertyInfo pi in pis)
            {
                if (this.classMap.Properties.ContainsKey(pi.Name))
                {
                    this.properties.Add(this.classMap.Properties[pi.Name].ColumnName, pi);
                }
            }
        }