예제 #1
0
        public List <T> Find <T>(Expression <Func <T, bool> > lambda) where T : EntityBase, new()
        {
            Type   type = typeof(T);
            string sql  = SqlCacheBuilder <T> .GetSql(SqlCacheBuilderType.FindAll);

            ConvertToSqlExpressionVisitor visitor = new ConvertToSqlExpressionVisitor();

            visitor.Visit(lambda);
            string where = visitor.GetWhere();
            sql         += where;
            return(this.ExecuteSql <List <T> >(sql, null, cmd =>
            {
                List <T> list = new List <T>();
                var reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    T t = new T();
                    foreach (var item in type.GetProperties())
                    {
                        string propertyName = item.GetMappingName();
                        item.SetValue(t, reader[propertyName] is DBNull ? null : reader[propertyName]);
                    }
                    list.Add(t);
                }
                return list;
            }, SqlConnectionPool.GetConnectionString(DBOPerateType.Write)));
        }
예제 #2
0
        //private static string _ConnectionStringDemo = ORMConfigurationManager.SqlConnStringDemo;

        public T Find <T>(long id) where T : EntityBase, new()
        {
            Type   type = typeof(T);
            string sql  = SqlCacheBuilder <T> .GetSql(SqlCacheBuilderType.FindOne);

            SqlParameter[] sqlParams = new SqlParameter[]
            {
                new SqlParameter("@ID", id)
            };
            return(this.ExecuteSql <T>(sql, sqlParams, cmd =>
            {
                var reader = cmd.ExecuteReader();
                if (reader.Read())
                {
                    T t = new T();
                    foreach (var item in type.GetProperties())
                    {
                        string propertyName = item.GetMappingName();
                        item.SetValue(t, reader[propertyName] is DBNull ? null : reader[propertyName]);
                    }
                    return t;
                }
                return null;
            }, SqlConnectionPool.GetConnectionString(DBOPerateType.Write)));
        }
예제 #3
0
        public long Insert <T>(T t) where T : EntityBase, new()
        {
            Type type = typeof(T);

            string sql = SqlCacheBuilder <T> .GetSql(SqlCacheBuilderType.Insert);

            var sqlParams = type.GetProperties().Select(r => new SqlParameter($"@{r.GetMappingName()}", r.GetValue(t) ?? DBNull.Value)).ToArray();

            //注意指定的返回值泛型类型要和方法的返回值类型一致
            return(this.ExecuteSql <long>(sql, sqlParams, cmd =>
            {
                object result = cmd.ExecuteScalar();//这里必须在insert语句后加一句查询id
                long id = 0L;
                long.TryParse(result.ToString(), out id);
                return id;
            }, SqlConnectionPool.GetConnectionString(DBOPerateType.Write)));//因为懒得建立slave库,就全用写的了
        }
예제 #4
0
        public bool Delete <T>(T t) where T : EntityBase, new()
        {
            Type   typte = typeof(T);
            string sql   = SqlCacheBuilder <T> .GetSql(SqlCacheBuilderType.Delete);

            var sqlParams = new SqlParameter("@ID", t.Id);

            return(this.ExecuteSql <bool>(sql, new SqlParameter[] { sqlParams }, cmd => cmd.ExecuteNonQuery() == 1, SqlConnectionPool.GetConnectionString(DBOPerateType.Write)));
        }
예제 #5
0
        public bool Update <T>(T t) where T : EntityBase, new()
        {
            Type   type = typeof(T);
            string sql  = SqlCacheBuilder <T> .GetSql(SqlCacheBuilderType.Update);

            var SqlParams = type.GetProperties().Select(r => new SqlParameter($"@{r.GetMappingName()}", r.GetValue(t) ?? DBNull.Value)).ToArray();

            return(this.ExecuteSql <bool>(sql, SqlParams, cmd => cmd.ExecuteNonQuery() == 1, SqlConnectionPool.GetConnectionString(DBOPerateType.Write)));
        }