Пример #1
0
        //list转json

        public static string ListToJson <T>(string jsonName, IList <T> entity, string strUserJson)
        {
            StringBuilder Json = new StringBuilder();

            Json.Append("{" + strUserJson + "\"" + jsonName + "\":[");

            if (entity.Count > 0)
            {
                for (int i = 0; i < entity.Count; i++)
                {
                    // T obj = Activator.CreateInstance<T>();

                    //Type type = obj.GetType();

                    //PropertyInfo[] pis = type.GetProperties();

                    TableInfo tfInfo = EntityTypeCache.GetTableInfo(typeof(T));
                    IDictionary <string, ColumnAttribute> dicColumn = tfInfo.DicColumns;

                    Json.Append("{");
                    int j = 0;

                    foreach (string key in dicColumn.Keys)
                    {
                        Json.Append("\"" + dicColumn[key].Name + "\":\"" + (EntityFactory.GetPropertyValue(entity[i], key) == null ?
                                                                            System.DBNull.Value : (object)System.Web.HttpUtility.HtmlEncode(EntityFactory.GetPropertyValue(entity[i], key).ToString())) + "\"");

                        if (j++ < dicColumn.Keys.Count - 1)
                        {
                            Json.Append(",");
                        }

                        else
                        {
                            Json.Append("}");
                        }
                    }

                    if (i < entity.Count - 1)
                    {
                        Json.Append(",");
                    }
                }
            }

            Json.Append("]}");

            return(Json.ToString());
        }
Пример #2
0
        /// <summary>
        /// 根据一个泛型类型查询一个集合

        /// </summary>
        /// <typeparam name="T">泛型类型</typeparam>
        /// <param name="reader">只读数据流</param>
        /// <returns></returns>
        public IList <T> ConvertToList <T>(IDataReader reader) where T : class
        {
            try
            {
                T         entity = default(T);
                IList <T> list   = new List <T>();
                TableInfo tfInfo = EntityTypeCache.GetTableInfo(typeof(T));

                //行循环处理

                while (reader.Read())
                {
                    entity = EntityFactory.CreateInstance <T>();

                    //所有列处理
                    foreach (string key in tfInfo.DicColumns.Keys)
                    {
                        //列存在,而且不为空

                        if (reader.GetSchemaTable().Select("ColumnName='" + key + "'").Length > 0 &&
                            reader[key] != DBNull.Value)
                        {
                            //if (tfInfo.DicColumns[key].DataType == Common.DbCommon.DataType.Datetime)
                            //{
                            //    //int intTimeZone = 8;
                            //    //if (HttpContext.Current.Request.Cookies["ZTETIMEZONE"]!=null)
                            //    //{
                            //    //    intTimeZone = int.Parse(HttpContext.Current.Request.Cookies["ZTETIMEZONE"].Value);
                            //    //}
                            //    //DateTime dt = Convert.ToDateTime(reader[key]).AddHours(intTimeZone-8);
                            //    tfInfo.DicProperties[key].SetValue(entity, reader[key], null);
                            //}
                            //else
                            //{


                            //}

                            tfInfo.DicProperties[key].SetValue(entity, reader[key], null);
                        }
                    }

                    //关联表处理

                    if (tfInfo.DicLinkTable.Keys.Count > 0)
                    {
                        foreach (string key in tfInfo.DicLinkTable.Keys)
                        {
                            Type             entityType = tfInfo.DicLinkTable[key].DataType;
                            string           sql        = Factory.CreateSingleSql(entityType);
                            IDataParameter[] param      = new IDataParameter[] {
                                new SqlParameter()
                            };
                            param[0].ParameterName = "@"
                                                     + EntityTypeCache.GetTableInfo(tfInfo.DicLinkTable[key].DataType).Table.PrimaryKeyName;
                            param[0].Value = EntityFactory.GetPropertyValue(entity, tfInfo.DicLinkTable[key].KeyName);
                            using (IDbProvider provider = new SqlProvider())
                            {
                                IDataReader read        = ExecuteDataReader(sql, param);
                                object      entityChild = EntityFactory.CreateInstance(entityType, false);
                                if (read.Read())
                                {
                                    foreach (string propertyName
                                             in EntityTypeCache.GetTableInfo(entityType).DicProperties.Keys)
                                    {
                                        EntityTypeCache.GetTableInfo(entityType).DicProperties[propertyName].SetValue(
                                            entityChild,
                                            read[EntityTypeCache.GetTableInfo(entityType).DicColumns[propertyName].Name],
                                            null);
                                    }
                                }
                                tfInfo.DicProperties[key].SetValue(entity, entityChild, null);
                            }
                        }
                    }
                    list.Add(entity);
                }
                return(list);
            }
            finally
            {
                reader.Close();
            }
        }