Example #1
0
        /// <summary>
        /// 查询一行记录
        /// </summary>
        /// <param name="filter">过滤器</param>
        /// <returns>返回单条数据</returns>
        public CModel GetModel(ModelFilter filter)
        {
            string sql = filter.ExecuteSql.ToString();

            CModel model = null;

            try
            {
                SqlDataReader reader = SqlRequest.GetReader(sql, filter.GetParams());

                while (reader.Read())
                {
                    model = new CModel(filter.TableName);

                    foreach (var field in filter.Fields)
                    {
                        model[field] = reader[field];
                    }
                }


                reader.Close();

                return(model);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Example #2
0
        /// <summary>
        /// 查询一行记录
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns>返回一条记录</returns>
        public T GetModel <T>(ModelFilter filter)
        {
            string sql = filter.ExecuteSql.ToString();

            CModel model = null;

            try
            {
                SqlDataReader reader = SqlRequest.GetReader(sql, filter.GetParams());

                while (reader.Read())
                {
                    model = new CModel(filter.TableName);

                    foreach (var field in filter.Fields)
                    {
                        model[field] = reader[field];
                    }
                }


                T m_obj = default(T);

                reader.Close();

                if (model == null)
                {
                    return(m_obj);
                }

                //string str = JsonConverter.SerializeObject(model.FieldsValue);

                //m_obj = JsonConverter.JsonDeserialize<T>(str);


                m_obj = JsonConverter.CModelToModel <T>(model);

                return(m_obj);
            }
            catch (Exception ex)
            {
                throw new Exception("查询一行数据出错了", ex);
            }
        }