2023年6月21日发(作者:)

C#使⽤反射组建动态sqlC#使⽤反射组建动态sql ///

///

使⽤反射组建动态查询SQL并查询 ///

/// 查询表名 /// 参数对象 /// public static DataTable ExecuteReaderByObj(string tableName,object param) { DataTable dataTable; try { StringBuilder sb = new StringBuilder($"select * from {tableName} where 1=1 "); List paramList = new List(); //

使⽤反射组建动态SQL foreach (PropertyInfo p in e().GetProperties()) { if (ue(param) != null && !OrEmpty(ue(param).ToString())) { // string默认使⽤模糊查询 if ( == typeof(string).Name) { ($"and {} like @{} "); (new SqlParameter(, $"%{ue(param)}%")); } else { ($"and {}=@{} "); (new SqlParameter(, ue(param))); } } } var sql = ng(); dataTable = ExecuteReader(sql, y()).Tables[0]; } catch (Exception e) { throw new Exception($"查询出现异常,原因为:{e}"); } return dataTable; }

2023年6月21日发(作者:)

C#使⽤反射组建动态sqlC#使⽤反射组建动态sql ///

///

使⽤反射组建动态查询SQL并查询 ///

/// 查询表名 /// 参数对象 /// public static DataTable ExecuteReaderByObj(string tableName,object param) { DataTable dataTable; try { StringBuilder sb = new StringBuilder($"select * from {tableName} where 1=1 "); List paramList = new List(); //

使⽤反射组建动态SQL foreach (PropertyInfo p in e().GetProperties()) { if (ue(param) != null && !OrEmpty(ue(param).ToString())) { // string默认使⽤模糊查询 if ( == typeof(string).Name) { ($"and {} like @{} "); (new SqlParameter(, $"%{ue(param)}%")); } else { ($"and {}=@{} "); (new SqlParameter(, ue(param))); } } } var sql = ng(); dataTable = ExecuteReader(sql, y()).Tables[0]; } catch (Exception e) { throw new Exception($"查询出现异常,原因为:{e}"); } return dataTable; }