public int FindCount <T>(string propertyName, object propertyValue) where T : new() { int count = 0; try { PropertyInfo[] properties = ReflectionHelper.GetProperties(new T().GetType()); TableInfo tableInfo = EntityHelper.GetTableInfo(new T(), DbOperateType.COUNT, properties); string strSql = EntityHelper.GetFindCountSql(tableInfo); strSql += string.Format(" WHERE {0} = @{1}", propertyName, propertyName); ColumnInfo columnInfo = new ColumnInfo(); columnInfo.Add(propertyName, propertyValue); IDbDataParameter[] parameters = DbFactory.CreateDbParameters(1); EntityHelper.SetParameters(columnInfo, parameters); count = Convert.ToInt32(AdoHelper.ExecuteScalar(AdoHelper.ConnectionString, CommandType.Text, strSql, parameters)); } catch (Exception ex) { throw ex; } return(count); }
public List <T> FindByProperty <T>(string propertyName, object propertyValue) where T : new() { List <T> list = new List <T>(); IDataReader sdr = null; try { PropertyInfo[] properties = ReflectionHelper.GetProperties(new T().GetType()); TableInfo tableInfo = EntityHelper.GetTableInfo(new T(), DbOperateType.SELECT, properties); String strSql = EntityHelper.GetFindAllSql(tableInfo); strSql += string.Format(" WHERE {0} = @{1}", propertyName, propertyName); strSql = strSql.ToLower(); String columns = SQLBuilderHelper.fetchColumns(strSql);// strSql.Substring(0, strSql.IndexOf("FROM")); ColumnInfo columnInfo = new ColumnInfo(); columnInfo.Add(propertyName, propertyValue); IDbDataParameter[] parameters = DbFactory.CreateDbParameters(1); EntityHelper.SetParameters(columnInfo, parameters); sdr = AdoHelper.ExecuteReader(AdoHelper.ConnectionString, CommandType.Text, strSql, parameters); list = EntityHelper.toList <T>(sdr, tableInfo, properties); } catch (Exception ex) { throw ex; } finally { if (sdr != null) { sdr.Close(); } } return(list); }
public List <T> FindByProperty <T>(string propertyName, object propertyValue) where T : new() { List <T> listArr = new List <T>(); IDataReader sdr = null; try { PropertyInfo[] properties = ReflectionHelper.GetProperties(new T().GetType()); TableInfo tableInfo = EntityHelper.GetTableInfo(new T(), DbOperateType.SELECT); String strSql = EntityHelper.GetFindAllSql(tableInfo); if (DatabaseType.ORACLE == AdoHelper.DbType) { strSql += string.Format(" WHERE {0} = :{1}", propertyName, propertyName); } else { strSql += string.Format(" WHERE {0} = :{1}", propertyName, propertyName); } strSql = strSql.ToUpper(); String columns = strSql.Substring(0, strSql.IndexOf("FROM")); ColumnInfo columnInfo = new ColumnInfo(); columnInfo.Add(propertyName, propertyValue); IDbDataParameter[] parameters = DbFactory.CreateDbParameters(1); EntityHelper.SetParameters(columnInfo, parameters); sdr = AdoHelper.ExecuteReader(AdoHelper.ConnectionString, CommandType.Text, strSql, parameters); while (sdr.Read()) { T entity = new T(); foreach (PropertyInfo property in properties) { if (EntityHelper.IsCaseColumn(property, DbOperateType.SELECT)) { continue; } String name = tableInfo.PropToColumn[property.Name].ToString(); if (columns.Contains(name.ToUpper()) || columns.Contains("*")) { ReflectionHelper.SetPropertyValue(entity, property, sdr[name]); } } listArr.Add(entity); } } catch (Exception ex) { throw ex; } finally { if (sdr != null) { sdr.Close(); } } return(listArr); }