Exemplo n.º 1
0
        /// <summary>
        /// 返回查询语句(废用)
        /// </summary>
        /// <param name="type"></param>
        /// <param name="filter"></param>
        /// <returns></returns>
        public static string GetSelect(Type type, ModelFilter filter)
        {
            PropertyInfo[] fields = type.GetProperties();

            StringBuilder sql = new StringBuilder();

            sql.Append("SELECT ");

            int i = 0;

            foreach (var field in fields)
            {
                sql.Append(field.Name);

                if (i++ < fields.Length - 1)
                {
                    sql.Append(",");
                }
            }

            sql.Append(" FROM ");

            sql.Append(type.Name);

            return(sql.ToString());
        }
Exemplo n.º 2
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;
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// 删除一行或多行记录
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="filter"></param>
        public void DeleteModel(ModelFilter filter)
        {
            string sql = SqlSplice.GetDelete(filter);

            try
            {
                SqlRequest.Update(sql, filter.Params.ToArray());
            }
            catch (Exception ex)
            {
                throw new Exception("删除记录出错了", ex);
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// 查询多行记录
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="filter">过滤器</param>
        /// <returns></returns>
        public List <T> GetModels <T>(ModelFilter filter)
        {
            Type type = typeof(T);

            string sql = filter.ExecuteSql.ToString();

            List <CModel> models = new List <CModel>();

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

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

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

                    models.Add(model);
                }

                reader.Close();


                List <T> m_objs = new List <T>();

                if (models.Count <= 0)
                {
                    return(m_objs);
                }

                foreach (var model in models)
                {
                    T m_obj = JsonConverter.CModelToModel <T>(model);

                    m_objs.Add(m_obj);
                }

                return(m_objs);
            }
            catch (Exception ex)
            {
                throw new Exception("查询多行记录出错了", ex);
            }
        }
Exemplo n.º 5
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);
            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// 返回删除语句
        /// </summary>
        /// <param name="filter">过滤器</param>
        /// <returns></returns>
        public static string GetDelete(ModelFilter filter)
        {
            StringBuilder sql = new StringBuilder();

            List <string> param_names = filter.ParamNames;

            sql.Append($"DELETE FROM {filter.TableName} WHERE ");

            int i = 0;

            foreach (var param in param_names)
            {
                if (i++ > 0)
                {
                    sql.Append(",");
                }

                sql.Append($"{param}=@{param}");
            }


            return(sql.ToString());
        }