コード例 #1
0
        public List <T> GetDataList <T>(string exp, string orderExp, bool isDesc = true)
        {
            var typeT = typeof(T);
            TableMappingAttribute attribute = BaseDaoAttribute.GetTableMappingAttribute(typeT);

            if (attribute == null)
            {
                return(null);
            }

            var sql  = $"SELECT *FROM dbo.{attribute.TableName} WHERE {exp}";
            var para = new Hashtable();

            para.Add("sql", sql);
            return(QueryForList <T>($"Query{attribute.TableName}", para));
        }
コード例 #2
0
        /*
         * public void CustomAdd<T>(T t, List<SystemObjectFields> fields)
         * {
         *  //找到表名称
         *  var typeT = typeof(T);
         *  TableMappingAttribute attribute = BaseDaoAttribute.GetTableMappingAttribute(typeT);
         *  if (attribute == null)
         *      return;
         *
         *  //新增用反射把各个字段赋值,动态创建插入语句
         *  List<string> list1 = new List<string>();
         *  List<string> list2 = new List<string>();
         *  var properties = typeT.GetProperties();
         *  foreach (var propertiy in properties)
         *  {
         *      var tmp = fields.FirstOrDefault(c => c.FieldName == propertiy.Name && !c.PKConstraint);
         *      if (tmp != null)
         *      {
         *          list1.Add($"[{propertiy.Name}]");
         *          var propertyType = propertiy.PropertyType;
         *          var newVal = TypeConversionTool.ConvertToTarget(tmp.TypeName, propertyType, propertiy.GetValue(t));
         *          var result = string.IsNullOrEmpty(newVal.Item2) ? newVal.Item1 : "";
         *          list2.Add($"'{result}'");
         *      }
         *
         *  }
         *  string mainSql = string.Join(",", list1);
         *  var subSql = string.Join(",", list2);
         *  var sql = $@"INSERT INTO [dbo].{attribute.TableName}
         *             ({mainSql})
         *       VALUES
         *             ({subSql})";
         *
         *  var para = new Hashtable();
         *  para.Add("sql", sql);
         *  this.Add($"Add{attribute.TableName}", sql);
         * }
         */
        #endregion

        #region Custom Update

        /*
         * public void CustomUpdate<T>(T t, List<SystemObjectFields> fields)
         * {
         * //找到表名称
         * var typeT = typeof(T);
         * TableMappingAttribute attribute = BaseDaoAttribute.GetTableMappingAttribute(typeT);
         * if (attribute == null)
         *  return;
         *
         * List<string> updateColumns = new List<string>();
         * var properties = typeT.GetProperties();
         *
         * foreach (var propertiy in properties)
         * {
         *  var tmp = fields.FirstOrDefault(c => c.FieldName == propertiy.Name && !c.PKConstraint);
         *  if (tmp != null)
         *  {
         *      var propertyType = propertiy.PropertyType;
         *      var str = $"[{tmp.FieldName}]='{TypeConversionTool.ConvertToTarget(tmp.TypeName, propertyType, propertiy.GetValue(t)).Item1}'";
         *      updateColumns.Add(str);
         *  }
         *
         * }
         *
         * var PKConstraintColumnName = fields.FirstOrDefault(c => c.PKConstraint);
         * var pType = properties.FirstOrDefault(c => c.Name == PKConstraintColumnName.FieldName);
         * var PKConstraintColumnValue = TypeConversionTool.ConvertToTarget(PKConstraintColumnName.TypeName, pType.PropertyType, pType.GetValue(t)).Item1;
         *
         * var sql = $@" update {attribute.TableName} set {string.Join(",", updateColumns)} where {PKConstraintColumnName.FieldName}='{PKConstraintColumnValue}' ";
         * var para = new Hashtable();
         * para.Add("sql", sql);
         *
         * this.Update($"Update{attribute.TableName}", para);
         * }
         */
        #endregion

        #region Custom Delete
        public int CustomDelete <T>(List <int> ids)
        {
            //找到表名称
            var typeT = typeof(T);
            TableMappingAttribute attribute = BaseDaoAttribute.GetTableMappingAttribute(typeT);

            if (attribute == null)
            {
                return(-1);
            }

            var sql  = $@"DELETE FROM {attribute.TableName}  WHERE Id IN ({string.Join(",", ids)})";
            var para = new Hashtable();

            para.Add("sql", sql);
            return(this.Delete($"Delete{attribute.TableName}", para));
        }
コード例 #3
0
        public T GetOne <T>(int id)
        {
            var typeT = typeof(T);

            TableMappingAttribute attribute = BaseDaoAttribute.GetTableMappingAttribute(typeT);

            if (attribute == null)
            {
                return(default(T));
            }



            var sql = $@"SELECT *FROM {attribute.TableName} where id={id} ";

            var param = new Hashtable();

            param.Add("sql", sql);
            return(QueryForObject <T>($"GetOne{attribute.TableName}", param));
        }