コード例 #1
0
        /// <summary>
        /// 插入实体
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool Insert(T entity)
        {
            if (!entity.Validate())
            {
                throw new Exception("验证未通过");
            }

            var type  = typeof(T);
            var props = type.GetProperties().NoKey();
            var sql   = SqlBulider <T> .GetSql(SqlType.Insert);

            var parameters = props.Select(a => new SqlParameter($"@{a.GetName<MyPropertyAttribute>()}", a.GetValue(entity) ?? DBNull.Value)).ToArray();

            return(ExecuteSql <bool>(sql, parameters, SqlConnectionType.Write, command =>
            {
                return command.ExecuteNonQuery() > 0;
            }));

            #region 封装前
            //using (SqlConnection conn = new SqlConnection(_connStr))
            //{
            //    conn.Open();
            //    var command = new SqlCommand(sql, conn);
            //    foreach (var prop in props)
            //    {
            //        var name = prop.GetName<MyPropertyAttribute>();
            //        command.Parameters.Add(new SqlParameter($"@{name}", prop.GetValue(entity)));
            //    }
            //    isSuccess = command.ExecuteNonQuery() > 0;
            //}
            //return isSuccess;
            #endregion
        }
コード例 #2
0
        /// <summary>
        /// 更新实体
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool Update(T entity)
        {
            var type  = typeof(T);
            var props = type.GetProperties();
            var sql   = SqlBulider <T> .GetSql(SqlType.Update);

            var parameters = props.Select(a => new SqlParameter($"@{a.GetName<MyPropertyAttribute>()}", a.GetValue(entity) ?? DBNull.Value)).ToArray();

            return(ExecuteSql(sql, parameters, SqlConnectionType.Write, command =>
            {
                return command.ExecuteNonQuery() > 0;
            }));
        }
コード例 #3
0
        /// <summary>
        /// 删除实体
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool Delete(int id)
        {
            var type = typeof(T);
            var sql  = SqlBulider <T> .GetSql(SqlType.Delete);

            var parameters = new List <SqlParameter>()
            {
                new SqlParameter($"@{SqlBulider<T>.KeyName}", id)
            };

            return(ExecuteSql(sql, parameters.ToArray(), SqlConnectionType.Write, command =>
            {
                return command.ExecuteNonQuery() > 0;
            }));
        }
コード例 #4
0
        /// <summary>
        /// 查找实体
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public T Find(int id)
        {
            var type = typeof(T);
            var sql  = SqlBulider <T> .GetSql(SqlType.Find);

            var parameters = new List <SqlParameter>()
            {
                new SqlParameter($"@{SqlBulider<T>.KeyName}", id)
            };

            return(ExecuteSql(sql, parameters.ToArray(), SqlConnectionType.Read, command =>
            {
                var reader = command.ExecuteReader();
                return ConvertEntity(reader);
            }));

            #region 封装前
            //using (SqlConnection conn = new SqlConnection(_connStr))
            //{
            //    conn.Open();
            //    var command = new SqlCommand(sql, conn);
            //    command.Parameters.Add(new SqlParameter($"@{SqlBulider<T>.KeyName}", id));
            //    var reader = command.ExecuteReader();
            //    if (reader.Read())
            //    {
            //        var entity = new T();
            //        var props = type.GetProperties();
            //        foreach (var prop in props)
            //        {
            //            prop.SetValue(entity, reader[prop.Name]);
            //        }
            //        return entity;
            //    }
            //}
            //return default;
            #endregion
        }