Exemplo n.º 1
0
        /// <summary>
        /// 批量添加
        /// </summary>
        /// <param name="Model"></param>
        /// <returns></returns>
        public bool GDAddBatch <T>(List <T> ListModel) where T : new()
        {
            List <string>           ListValue     = new List <string>();
            List <SqlParameter>     ListObject    = new List <SqlParameter>();
            List <GDModel.SQLModel> ListParameter = null;
            int n = 0;
            int c = ListModel.Count;

            foreach (var item in ListModel)
            {
                SQLPropertyInfo PropertyInfos = new SQLPropertyInfo();
                ListParameter = PropertyInfos.GDGetPropertys(item, BaseType, n);
                if (ListParameter == null || ListParameter.Count == 0)
                {
                    return(false);
                }
                ListParameter = ListParameter.Where(w => !w.IsIncrease).ToList();
                string[] valuestr = new string[ListParameter.Count];
                for (int i = 0; i < valuestr.Length; i++)
                {
                    valuestr[i] = ListParameter[i].ParameterName;
                    ListObject.Add(ListParameter[i].Parameter as SqlParameter);
                }
                ListValue.Add($"({string.Join(",", valuestr)})");
                n++;
            }
            return(BsExecute($"insert into {typeof(T).Name}({string.Join(",", ListParameter.Select(s => s.Field).ToList())}) values {string.Join(",", ListValue)}", c, ListObject.ToArray()));
        }
Exemplo n.º 2
0
        /// <summary>
        /// 分页
        /// </summary>
        /// <typeparam name="T">对象</typeparam>
        /// <param name="Source">List</param>
        /// <param name="Exp">查询条件</param>
        /// <returns></returns>
        public GDPaging <T> GDPaging <T>(T Source, Expression <Func <T, bool> > Exp, int ThisPage = 1, int PageSize = 10) where T : new()
        {
            T               t             = new T();
            string          Where         = Exp.DealExpress();
            SQLPropertyInfo PropertyInfos = new SQLPropertyInfo();
            string          Sort          = PropertyInfos.GDGetIsIncrease(t) + " Desc";
            GDPaging <T>    GDModel       = new GDPaging <T>();
            List <T>        lt            = new List <T>();

            SqlParameter[] parameter =
            {
                new SqlParameter("@ThisPage",  ThisPage),
                new SqlParameter("@PageSize",  PageSize),
                new SqlParameter("@TableName", typeof(T).Name),
                new SqlParameter("@Condition", string.IsNullOrEmpty(Where)?"":(" and " + Where)),
                new SqlParameter("@Sorting",   Sort)
            };
            DataSet ds = BsStorageDs("GT_Paging", parameter);

            if (ds == null || ds.Tables.Count < 2 || ds.Tables[0].Rows.Count == 0)
            {
                GDModel.GDModel = new List <T>();
                return(GDModel);
            }
            else
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    lt.Add(ds.Tables[0].Rows[i].ToModel <T>());
                }
                GDModel.GDModel = lt;
                GDModel.Count   = Convert.ToInt32(ds.Tables[1].Rows[0][0]);
            }
            return(GDModel);
        }
Exemplo n.º 3
0
        /// <summary>
        /// 分页
        /// </summary>
        /// <typeparam name="T">对象</typeparam>
        /// <param name="Source">List</param>
        /// <returns></returns>
        public GDPaging <T> GDPaging <T>(IEnumerable <T> Source, int ThisPage = 1, int PageSize = 10) where T : new()
        {
            T               t             = new T();
            string          Where         = Source.ToString().Contains("Where") ? (Source.AsQueryable().Expression).DealExpress() : "";
            SQLPropertyInfo PropertyInfos = new SQLPropertyInfo();
            string          Sort          = PropertyInfos.GDGetIsIncrease(t) + " Desc";
            GDPaging <T>    GDModel       = new GDPaging <T>();
            List <T>        lt            = new List <T>();

            SqlParameter[] parameter =
            {
                new SqlParameter("@ThisPage",  ThisPage),
                new SqlParameter("@PageSize",  PageSize),
                new SqlParameter("@TableName", typeof(T).Name),
                new SqlParameter("@Condition", Where),
                new SqlParameter("@Sorting",   Sort)
            };
            DataSet ds = BsStorageDs("GT_Paging", parameter);

            if (ds == null || ds.Tables.Count < 2 || ds.Tables[0].Rows.Count == 0)
            {
                GDModel.GDModel = new List <T>();
                return(GDModel);
            }
            else
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    lt.Add(ds.Tables[0].Rows[i].ToModel <T>());
                }
                GDModel.GDModel = lt;
                GDModel.Count   = Convert.ToInt32(ds.Tables[1].Rows[0][0]);
            }
            return(GDModel);
        }
Exemplo n.º 4
0
        /// <summary>
        /// 根据Key删除
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="Model"></param>
        /// <returns></returns>
        public bool GDeleteByKey <T>(T Model) where T : new()
        {
            SQLPropertyInfo         PropertyInfos = new SQLPropertyInfo();
            List <GDModel.SQLModel> ListParameter = PropertyInfos.GDGetPropertys(Model, BaseType);
            var Keys = ListParameter.Where(w => w.IsKey).Select(s => new { Field = s.Field, Value = s.Value }).ToList().First();

            return(BsExecute($"DELETE FROM {typeof(T).Name} WHERE {Keys.Field + "=" + Keys.Value.ToValStr()}"));
        }
Exemplo n.º 5
0
        /// <summary>
        /// 根据Key更新
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="Model"></param>
        /// <param name="Exp"></param>
        /// <returns></returns>
        public bool GDUpdateByKey <T>(T Model) where T : new()
        {
            SQLPropertyInfo         PropertyInfos = new SQLPropertyInfo();
            List <GDModel.SQLModel> ListParameter = PropertyInfos.GDGetPropertys(Model, BaseType);
            var Keys     = ListParameter.Where(w => w.IsKey).Select(s => new { Field = s.Field, Value = s.Value }).ToList().First();
            var GetColum = ListParameter.Where(w => !w.IsKey).Select(s => new { Field = (s.Field + "=" + s.ParameterName), Parameter = s.Parameter }).ToList();

            return(BsExecute($"UPDATE {typeof(T).Name} SET {string.Join(",", GetColum.Select(s => s.Field).ToList())} where {Keys.Field + "=" + Keys.Value}", GetColum.Select(s => s.Parameter as SqlParameter).ToArray()));
        }
Exemplo n.º 6
0
        /// <summary>
        /// 获取多条数据无排序,默认排序为主键倒序
        /// </summary>
        /// <typeparam name="T">对象</typeparam>
        /// <param name="Source">对象</param>
        /// <returns></returns>
        public List <T> GDList <T>(T Source, Expression <Func <T, bool> > Exp) where T : new()
        {
            T               t             = new T();
            string          Where         = Exp.DealExpress();
            SQLPropertyInfo PropertyInfos = new SQLPropertyInfo();
            string          Sort          = PropertyInfos.GDGetIsIncrease(t) + " Desc";
            DataTable       dt            = BsDataTable($"select * from {typeof(T).Name} where 1=1 {(string.IsNullOrEmpty(Where) ? "" : (" and " + Where))} Order By {Sort}");

            return(dt.ToModel <T>());
        }
Exemplo n.º 7
0
        /// <summary>
        /// 获取多条数据无排序,默认排序为主键倒序
        /// </summary>
        /// <typeparam name="T">对象</typeparam>
        /// <param name="Source">对象</param>
        /// <returns></returns>
        public List <T> GDList <T>(IEnumerable <T> Source) where T : new()
        {
            T               t             = new T();
            string          Where         = Source.ToString().Contains("Where") ? (Source.AsQueryable().Expression).DealExpress() : "";
            SQLPropertyInfo PropertyInfos = new SQLPropertyInfo();
            string          Sort          = PropertyInfos.GDGetIsIncrease(t) + " Desc";
            DataTable       dt            = BsDataTable($"select * from {typeof(T).Name} where 1=1 {Where} Order By {Sort}");

            return(dt.ToModel <T>());
        }
Exemplo n.º 8
0
        /// <summary>
        /// 更新操作
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="Model"></param>
        /// <param name="Exp"></param>
        /// <returns></returns>
        public bool GDUpdate <T>(T Model, Expression <Func <T, bool> > Exp = null) where T : new()
        {
            string Where = Exp.DealExpress();

            Where = string.IsNullOrEmpty(Where) ? "" : (" and " + Where);
            SQLPropertyInfo         PropertyInfos = new SQLPropertyInfo();
            List <GDModel.SQLModel> ListParameter = PropertyInfos.GDGetPropertys(Model, BaseType);
            var GetColum = ListParameter.Where(w => !w.IsKey).Select(s => new { Field = (s.Field + "=" + s.ParameterName), Parameter = s.Parameter }).ToList();

            return(BsExecute($"UPDATE {typeof(T).Name} SET {string.Join(",", GetColum.Select(s => s.Field).ToList())} where 1=1 {Where}", GetColum.Select(s => s.Parameter as SqlParameter).ToArray()));
        }
Exemplo n.º 9
0
        /// <summary>
        /// 新增
        /// </summary>
        /// <typeparam name="T">实体</typeparam>
        /// <param name="Model">实体</param>
        /// <returns></returns>
        public bool GDAdd <T>(T Model) where T : new()
        {
            SQLPropertyInfo         PropertyInfos = new SQLPropertyInfo();
            List <GDModel.SQLModel> ListParameter = PropertyInfos.GDGetPropertys(Model, BaseType);

            if (ListParameter == null || ListParameter.Count == 0)
            {
                return(false);
            }
            var GetColum = ListParameter.Where(w => !w.IsIncrease).Select(s => new { s.Field, s.Parameter }).ToList();

            return(BsExecute($"insert into {typeof(T).Name}({string.Join(",", GetColum.Select(s => s.Field).ToList())}) VALUES({string.Join(",", GetColum.Select(s => "@" + s.Field).ToList())})", GetColum.Select(s => s.Parameter as SqlParameter).ToArray()));
        }
Exemplo n.º 10
0
        /// <summary>
        /// 获取一条数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="Model"></param>
        /// <returns></returns>
        public T GDInfo <T>(T Model, Expression <Func <T, bool> > Exp = null) where T : new()
        {
            T      t     = new T();
            string Where = Exp.DealExpress();

            Where = string.IsNullOrEmpty(Where) ? "" : (" and " + Where);
            SQLPropertyInfo         PropertyInfos = new SQLPropertyInfo();
            List <GDModel.SQLModel> ListParameter = PropertyInfos.GDGetPropertys(Model, BaseType);
            DataRow dr = BsRows($"select * from {typeof(T).Name} where 1=1 {Where}");

            if (dr == null)
            {
                return(t);
            }
            else
            {
                return(dr.ToModel <T>());
            }
        }