Ejemplo n.º 1
0
        /// <summary>
        /// 根据主键获取一行记录
        /// </summary>
        /// <param name="table"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public CModel GetModelByKey(string table, object key)
        {
            CModel m_table = new CModel(table);

            string sql = SqlSplice.GetSelect(m_table, key);

            CModel model = null;

            try
            {
                SqlDataReader reader = SqlRequest.GetReader(sql, SqlSplice.SqlParams.ToArray());

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

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



                return(model);
            }
            catch (Exception ex)
            {
                throw new Exception("根据主键查询一行记录出错了", ex);
            }
        }
Ejemplo n.º 2
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);
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 查询多行记录
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public List <T> GetModels <T>()
        {
            Type type = typeof(T);

            string sql = SqlSplice.GetSelect(type);

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

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

                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);
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 更新一行记录
        /// </summary>
        /// <param name="model"></param>
        public void UpdateModel(CModel model)
        {
            if (model == null)
            {
                return;
            }

            string sql = SqlSplice.GetUpdate(model);

            try
            {
                SqlRequest.Update(sql, SqlSplice.GetSqlParams());
            }
            catch (Exception ex)
            {
                throw new Exception("更新一行记录出错了", ex);
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// 新增一行记录
        /// </summary>
        /// <param name="model"></param>
        public void InsertModel(CModel model)
        {
            if (model == null)
            {
                return;
            }

            string sql = SqlSplice.GetInsert(model);

            try
            {
                SqlRequest.Update(sql, SqlSplice.GetSqlParams());
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        /// 根据主键查询一行记录
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <returns></returns>
        public T GetModelByKey <T>(object key)
        {
            Type type = typeof(T);

            string sql = SqlSplice.GetSelect(type, key);

            CModel model = null;

            try
            {
                SqlDataReader reader = SqlRequest.GetReader(sql, SqlSplice.SqlParams.ToArray());

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

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


                T m_obj = default(T);

                reader.Close();

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

                //var t = Activator.CreateInstance(type);

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

                return(m_obj);
            }
            catch (Exception ex)
            {
                throw new Exception("根据主键查询一行记录出错了", ex);
            }
        }
Ejemplo n.º 7
0
        /// <summary>
        /// 删除多行记录
        /// </summary>
        /// <param name="models"></param>
        public void DeleteModels(List <CModel> models)
        {
            if (models.Count <= 0)
            {
                return;
            }

            foreach (var model in models)
            {
                string sql = SqlSplice.GetDelete(model);

                try
                {
                    SqlRequest.Update(sql, SqlSplice.GetSqlParams());
                }
                catch (Exception ex)
                {
                    throw new Exception("删除多行记录出错了", ex);
                }
            }
        }