Example #1
0
        public IList <TEntity> GetModelInfoList(Expression <Func <TEntity, bool> > exp)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append("select * from ");
            sb.Append(this.TableName);
            sb.Append(" where ");
            sb.Append(GetWhereStr(exp));
            sb.Append(" order by ");
            sb.Append(this.ColumInfo.Single(c => c.Value.IsSort).Value.Name);
            sb.Append(" desc");

            List <TEntity> list = new List <TEntity>();

            using (SqlDataReader sr = SQLDbHelper.ExecuteReader(sb.ToString()))
            {
                while (sr.Read())
                {
                    TEntity t = new TEntity();

                    PropertyInfo[] proarr = t.GetType().GetProperties().Where(p => this.ColumInfo.Where(c => c.Key == p.Name).Count() > 0).ToArray();

                    foreach (var item in proarr)
                    {
                        item.SetValue(t, sr[this.ColumInfo[item.Name].Name], null);
                    }

                    list.Add(t);
                }
            }

            return(list);
        }
Example #2
0
        public virtual TEntity GetModelInfoByKeys(string[] keys)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append("select * from ");
            sb.Append(this.TableName);
            sb.Append(" where ");
            //找出主键字段,顺序对应keys的顺序
            IEnumerable <KeyValuePair <string, ColumAttr> > tempkey = this.ColumInfo.Where(c => c.Value.IsPrim);

            for (int i = 0; i < keys.Count(); i++)
            {
                sb.Append("[");
                sb.Append(tempkey.ElementAt(i).Value.Name);
                sb.Append("]='");
                sb.Append(keys.ElementAt(i));
                sb.Append("' and");
            }

            sb = sb.Remove(sb.Length - "and".Length, "and".Length);

            TEntity t = new TEntity();

            PropertyInfo[] proarr = t.GetType().GetProperties().Where(p => this.ColumInfo.Where(c => c.Key == p.Name).Count() > 0).ToArray();

            using (SqlDataReader sr = SQLDbHelper.ExecuteReader(sb.ToString()))
            {
                if (sr.Read())
                {
                    foreach (var item in proarr)
                    {
                        item.SetValue(t, sr[this.ColumInfo[item.Name].Name], null);
                    }

                    return(t);
                }
            }

            return(null);
        }
Example #3
0
        public virtual IList <TEntity> GetModelInfoList(Expression <Func <TEntity, bool> > exp, int pageszie, int currentindex)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append("select top ");
            sb.Append(pageszie);
            sb.Append(" * from (select *, row_number() over(order by [");
            sb.Append(this.ColumInfo.First(c => c.Value.IsSort).Value.Name);
            sb.Append("] desc) as ROW from ");
            sb.Append(this.TableName);
            sb.Append(")tempdb where tempdb.ROW>");
            sb.Append((currentindex - 1) * pageszie);
            sb.Append(" and ");
            sb.Append(GetWhereStr(exp));
            sb.Append(" order by ");
            sb.Append(this.ColumInfo.Single(c => c.Value.IsSort).Value.Name);
            sb.Append(" desc");

            List <TEntity> list = new List <TEntity>();

            using (SqlDataReader sr = SQLDbHelper.ExecuteReader(sb.ToString()))
            {
                while (sr.Read())
                {
                    TEntity t = new TEntity();

                    PropertyInfo[] proarr = t.GetType().GetProperties().Where(p => this.ColumInfo.Where(c => c.Key == p.Name).Count() > 0).ToArray();

                    foreach (var item in proarr)
                    {
                        item.SetValue(t, sr[this.ColumInfo[item.Name].Name], null);
                    }

                    list.Add(t);
                }
            }

            return(list);
        }