在更新时,不需要更新所有字段,因此要排除主键
有些实体类字段或者表名与数据库字段不相符 解决办法:特性标记
public static class Extend { public static string GetMapping<T>(this T t) where T : MemberInfo //继承属性和类共同的父类 { if (t.IsDefined(typeof(BsAttributes), true))//如果找到BsAttributes,获取名称 { BsAttributes bs = (BsAttributes)t.GetCustomAttribute(typeof(BsAttributes), true); return bs.Getname(); } else { return t.Name; } } }
标记属性的特性类
[AttributeUsage(AttributeTargets.Property)] //属性 public class KeysAuthAtturbute : BsAttributes { public KeysAuthAtturbute(string name) : base(name) { } }
标记类名的特性类
[AttributeUsage(AttributeTargets.Class)] //类 public class TableAtturbute : BsAttributes { public TableAtturbute(string name):base(name) { } }
都继承BsAttributes 类
public class BsAttributes : Attribute { private string _name = null; public BsAttributes(string name) { this._name = name; } public virtual string Getname() { return this._name; } }
编写完成只需要在实体类标记·
贴上代码
public bool Update<T>(T t) { Type type = typeof(T); //PropertyInfoKeys 是自己写的排除主键的方法 string columString = string.Join(",", type.PropertyInfoKeys().Select(p => $"[{p.Name}]=@{p.Name}")); IEnumerable<SqlParameter> parameters = type.PropertyInfoKeys().Select(p => new SqlParameter($"@{p.GetMapping}", p.GetValue(t) ?? DBNull.Value)); string sql = $"update [{type.Name}] set {columString} where ID=@ID"; if (conn.State == System.Data.ConnectionState.Closed) { conn.Open(); } SqlCommand cmd = new SqlCommand(sql, conn); cmd.Parameters.AddRange(parameters.ToArray()); int result = cmd.ExecuteNonQuery(); return result==1; }