Beispiel #1
0
        /// <summary>
        ///Querying the corresponding data according to the primary key
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="id"></param>
        /// <returns></returns>
        public virtual DbEntity QueryById <T>(object id)
        {
            DbEntity dbEntity         = null;
            Type     type             = typeof(T);
            var      attributeBuilder = new AttributeBuilder();
            var      tableEntity      = attributeBuilder.GetTableInfo(type);
            var      pkColumn         = attributeBuilder.GetPkColumnInfo(type);

            if (pkColumn == null)
            {
                return(dbEntity);
            }
            pkColumn.ColumnValue = id;
            dbEntity             = new DbEntity()
            {
                DbType      = DbFactory.DbType,
                TableEntity = tableEntity
            };
            string dbOperator = DbFactory.GetDbOperator();
            List <IDbDataParameter> dbParams = new List <IDbDataParameter>();
            StringBuilder           sqlBuild = new StringBuilder("select * from {tableName} where {columnName}={dbOperator}{columnName}");

            sqlBuild.Replace("{tableName}", tableEntity.TableName);
            sqlBuild.Replace("{columnName}", pkColumn.ColumnName);
            sqlBuild.Replace("{dbOperator}", dbOperator);
            var dbParam = DbFactory.GetDbParam(pkColumn);

            dbParams.Add(dbParam);
            dbEntity.CommandText = sqlBuild.ToString();
            dbEntity.DbParams    = dbParams;
            return(dbEntity);
        }
Beispiel #2
0
        /// <summary>
        ///  Modifying the corresponding data based on the modified parameters and primary key values
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="updateObjParams"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public virtual DbEntity UpdateById <T>(object updateObjParams, object id)
        {
            DbEntity dbEntity = null;
            Type     type     = typeof(T);

            if (updateObjParams == null)
            {
                return(dbEntity);
            }
            if (id == null)
            {
                return(dbEntity);
            }
            var attributeBuilder = new AttributeBuilder();
            var tableEntity      = attributeBuilder.GetTableInfo(type);

            if (tableEntity == null)
            {
                return(dbEntity);
            }
            List <TableColumnAttribute> updateColumns = attributeBuilder.GetColumnInfos(updateObjParams);

            if (updateColumns == null)
            {
                return(dbEntity);
            }
            var pkColumn = attributeBuilder.GetPkColumnInfo(type);

            if (pkColumn == null)
            {
                return(dbEntity);
            }
            pkColumn.ColumnValue = id;
            dbEntity             = new DbEntity()
            {
                DbType      = DbFactory.DbType,
                TableEntity = tableEntity
            };
            string        dbOperator = DbFactory.GetDbOperator();
            var           dbParams   = new List <IDbDataParameter>();
            StringBuilder sqlBuild   = new StringBuilder("update {tableName} set {updateCriteria}  where {columnName}={dbOperator}{columnName}");

            sqlBuild.Replace("{tableName}", tableEntity.TableName);
            sqlBuild.Replace("{columnName}", pkColumn.ColumnName);
            sqlBuild.Replace("{dbOperator}", dbOperator);
            HandleUpdateParam(updateColumns, ref sqlBuild, ref dbParams);
            var dbParam = DbFactory.GetDbParam(pkColumn);

            dbParams.Add(dbParam);
            dbEntity.CommandText = sqlBuild.ToString();
            dbEntity.DbParams    = dbParams;
            return(dbEntity);
        }