public T GetEntity <T>(string condition) where T : new() { CheckConnState(); T t = new T(); Type type = t.GetType(); string tableName = type.Name; PropertyInfo[] pis = type.GetProperties(); string selectStr = "select * from " + tableName + " where " + condition; DataSet ds = SelectDsBySql(selectStr, Connection); if (DataTools.DsIsNotNull(ds)) { foreach (DataColumn col in ds.Tables[0].Columns) { if (ds.Tables[0].Rows[0][col.ColumnName] != DBNull.Value && type.GetProperty(col.ColumnName) != null) { type.GetProperty(col.ColumnName).SetValue(t, Convert.ChangeType(ds.Tables[0].Rows[0][col.ColumnName], type.GetProperty(col.ColumnName).PropertyType), null); } } } else { t = default(T); } return(t); }
public List <T> GetEntityList <T>(string condition, IDbConnection conn) where T : new() { CheckConnState(ref conn); List <T> list = null; Type type = typeof(T); string tableName = type.Name; PropertyInfo[] pis = type.GetProperties(); string selectStr = "select * from " + tableName + " where " + condition; DataSet ds = SelectDsBySql(selectStr, conn); if (DataTools.DsIsNotNull(ds)) { list = new List <T>(); foreach (DataRow row in ds.Tables[0].Rows) { T tt = new T(); foreach (DataColumn col in ds.Tables[0].Columns) { if (row[col] != DBNull.Value && type.GetProperty(col.ColumnName) != null) { type.GetProperty(col.ColumnName).SetValue(tt, Convert.ChangeType(row[col], type.GetProperty(col.ColumnName).PropertyType), null); } } list.Add(tt); } } return(list); }