public static PropertyFieldPair[] GetFields(Type modelType) { List <PropertyFieldPair> list = new List <PropertyFieldPair>(); PropertyInfo[] properties = modelType.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly); foreach (PropertyInfo property in properties) { DbFieldAttribute attribute = Attribute.GetCustomAttribute(property, typeof(DbFieldAttribute)) as DbFieldAttribute; if (attribute != null) { MethodInfo ms = property.GetSetMethod(); MethodInfo mg = property.GetGetMethod(); if (ms != null && !ms.IsAbstract && mg != null && !mg.IsAbstract) { PropertyFieldPair mf = new PropertyFieldPair(); if (string.IsNullOrWhiteSpace(attribute.FieldName)) { mf.FieldName = property.Name; } else { mf.FieldName = attribute.FieldName; } mf.Property = property; list.Add(mf); } } } return(list.ToArray()); }
protected virtual DbParameter GetParameter(object value, PropertyFieldPair pair) {//孙良旭 2012-08-15,参考GetUpdateCommand也补充lock lock (DbCommand) { DbParameter parameter = DbCommand.CreateParameter(); parameter.ParameterName = "@" + pair.FieldName; //商希超 2012-08-10 参数异常 if (pair.Property.PropertyType == typeof(UInt16)) { parameter.DbType = System.Data.DbType.Int16; parameter.Value = Convert.ToInt16(pair.Property.GetValue(value, null)); } else if (pair.Property.PropertyType == typeof(UInt32)) { parameter.DbType = System.Data.DbType.Int32; parameter.Value = Convert.ToInt32(pair.Property.GetValue(value, null)); } else if (pair.Property.PropertyType == typeof(UInt64)) { parameter.DbType = System.Data.DbType.Int64; parameter.Value = Convert.ToInt64(pair.Property.GetValue(value, null)); } else if (pair.Property.PropertyType == typeof(DateTime)) { //孙东升 2012-8-31 DateTime time = (DateTime)pair.Property.GetValue(value, null); if (time <= new DateTime(1753, 1, 1, 12, 0, 0)) { time = new DateTime(1753, 1, 1, 12, 0, 0); } parameter.Value = time; } else if (pair.Property.PropertyType == typeof(string)) { //孙东升 2012-8-31 object obj = pair.Property.GetValue(value, null); if (obj == null) { parameter.Value = DBNull.Value; } else { parameter.Value = obj.ToString(); } } else { parameter.Value = pair.Property.GetValue(value, null); } return(parameter); } }
protected override DbParameter GetParameter(object value, PropertyFieldPair pair) { DbParameter parameter = DbCommand.CreateParameter(); parameter.Value = pair.Property.GetValue(value, null); if (parameter.Value == null) { parameter.Value = string.Empty; } else if (pair.Property.PropertyType == typeof(DateTime)) { DateTime dt = (DateTime)parameter.Value; TimeSpan ts = dt.Subtract(new DateTime(1970, 1, 1)); parameter.Value = Convert.ToInt64(ts.TotalMilliseconds); } parameter.ParameterName = "?"; return(parameter); }