Esempio n. 1
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);
        }
Esempio n. 2
0
        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);
        }