2023年8月3日发(作者:)

C#根据反射⽣成sql语句(Update语句)今天有⼈问我Update语句怎么搞,想了⼀下⼤致思路就是⽤特性去标识⼀下,主键,然后再去⽤反射的⽅式拼sql语句。想着晚上再写,⼜想了想的确好久没写博客了,也好久没正⼉⼋经写代码了,就顺⼿给写了下来。⼀、主键特性///

/// 主键特性(在实体类的上⽅加这个特性,指定该类的主键名称) /// [AttributeUsage()] public class PrimaryKeyAttribute : Attribute { private PrimaryKeyAttribute() { } private string _name; /// /// 构造⽅法 /// /// public PrimaryKeyAttribute(string name) { _name = name; } /// /// 主键名称 /// public string Name { get { return _name; } } }把这个特性加到实体类的上⽅,并指定主键名称,就可以了,⽰例:⼆、拼写SQL语句public class SqlBuilderHelper { { /// /// 获得主键名称 /// /// /// private static string GetPK() where T : class { string pkName = ; Type objTye = typeof(T); PrimaryKeyAttribute pk; foreach (Attribute attr in tomAttributes(true)) { pk = attr as PrimaryKeyAttribute; if (pk != null) return ; } return pkName; } /// /// sql修改语句 /// /// /// /// /// public static string UpdateSql(T entity, string tableName) where T : class { if (entity == null || OrEmpty(tableName)) { return ; } string pkName = GetPK(); if (OrEmpty(pkName)) { return ; } string pkValue = ; StringBuilder sb = new StringBuilder(); ("update "); (tableName); (" set "); Type type = e(); PropertyInfo[] props = perties(); List paraList = new List(); foreach (var prop in props) { if ( == (string)pkName) { pkValue = (string)ue(entity); } else { (GetUpdatePara(prop, entity)); } } if ( == 0) { return ; } ((",", paraList)); if (OrEmpty(pkValue)) { throw new Exception("主键不能为空"); } } (" where "); (pkName); (" = "); Format("'{0}'", pkValue); return ng(); } /// /// 获得修改参数 /// /// /// private static string GetUpdatePara(PropertyInfo property, T entity) { StringBuilder sb = new StringBuilder(); Format(" {0}='{1}' ", , ue(entity)); return ng(); } }

2023年8月3日发(作者:)

C#根据反射⽣成sql语句(Update语句)今天有⼈问我Update语句怎么搞,想了⼀下⼤致思路就是⽤特性去标识⼀下,主键,然后再去⽤反射的⽅式拼sql语句。想着晚上再写,⼜想了想的确好久没写博客了,也好久没正⼉⼋经写代码了,就顺⼿给写了下来。⼀、主键特性///

/// 主键特性(在实体类的上⽅加这个特性,指定该类的主键名称) /// [AttributeUsage()] public class PrimaryKeyAttribute : Attribute { private PrimaryKeyAttribute() { } private string _name; /// /// 构造⽅法 /// /// public PrimaryKeyAttribute(string name) { _name = name; } /// /// 主键名称 /// public string Name { get { return _name; } } }把这个特性加到实体类的上⽅,并指定主键名称,就可以了,⽰例:⼆、拼写SQL语句public class SqlBuilderHelper { { /// /// 获得主键名称 /// /// /// private static string GetPK() where T : class { string pkName = ; Type objTye = typeof(T); PrimaryKeyAttribute pk; foreach (Attribute attr in tomAttributes(true)) { pk = attr as PrimaryKeyAttribute; if (pk != null) return ; } return pkName; } /// /// sql修改语句 /// /// /// /// /// public static string UpdateSql(T entity, string tableName) where T : class { if (entity == null || OrEmpty(tableName)) { return ; } string pkName = GetPK(); if (OrEmpty(pkName)) { return ; } string pkValue = ; StringBuilder sb = new StringBuilder(); ("update "); (tableName); (" set "); Type type = e(); PropertyInfo[] props = perties(); List paraList = new List(); foreach (var prop in props) { if ( == (string)pkName) { pkValue = (string)ue(entity); } else { (GetUpdatePara(prop, entity)); } } if ( == 0) { return ; } ((",", paraList)); if (OrEmpty(pkValue)) { throw new Exception("主键不能为空"); } } (" where "); (pkName); (" = "); Format("'{0}'", pkValue); return ng(); } /// /// 获得修改参数 /// /// /// private static string GetUpdatePara(PropertyInfo property, T entity) { StringBuilder sb = new StringBuilder(); Format(" {0}='{1}' ", , ue(entity)); return ng(); } }