|
由于每次数据库操作 返回的 数据库结果集 如:DataSet、DataTableReader、SqlDataReader 都有非常繁琐的 取值操作
例如:
[code=csharp]while (this.dr.Read())
{
string Name = this.dr["Name"].ToString();//医院名称
string Address = this.dr["Address"].ToString();//地址
string Features = this.dr["Features"].ToString();//特色项目
string Details = this.dr["Details"].ToString();//医院描述
string Favorable = this.dr["Favorable"].ToString();//折扣
string SmallImg = this.dr["SmallImg"].ToString();//医院小图片
}[/code]
所以写了一个反射帮助类,来解决这些繁琐的取值操做 (ps:结果有对象的实体对象)
部分代码如下:
[code=csharp]#region ************* 反射:将DataSet反射成泛型集合 *************
/// <summary>
/// 将DataSet反射成泛型集合
/// </summary>
/// <typeparam name="T">实体对象</typeparam>
/// <param name="ds">DataSet</param>
/// <returns></returns>
public List<T> getModelArrary<T>(DataSet ds) where T : new()
{
if (ds == null || ds.Tables.Count == 0 || ds.Tables[0].Rows.Count == 0)
{
return null;
}
T t = new T();
List<T> list = new List<T>();
PropertyInfo[] properties = t.GetType().GetProperties();
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
T model = new T();
foreach (var propertyInfo in properties)
{
string name = propertyInfo.Name;
if (ds.Tables[0].Rows[name] != null && ds.Tables[0].Rows[name].ToString() != "")
{
string value = ds.Tables[0].Rows[name].ToString();
SetModelValue(name, value, model);
}
}
list.Add(model);
}
ds.Dispose();
return list;
}
#endregion ************* 反射:将DataSet反射成泛型集合 *************
#region ************* 反射:将SqlDataReader反射成泛型集合 *************
/// <summary>
/// 将SqlDataReader反射成泛型集合
/// </summary>
/// <typeparam name="T">实体对象</typeparam>
/// <param name="ds">SqlDataReader</param>
/// <returns></returns>
public List<T> getModelArrary<T>(SqlDataReader dr) where T : new()
{
if (dr == null || dr.HasRows == false)
{
return null;
}
T t = new T();
List<T> list = new List<T>();
PropertyInfo[] properties = t.GetType().GetProperties();
while (dr.Read())
{
T model = new T();
foreach (var propertyInfo in properties)
{
string name = propertyInfo.Name;
if (dr[name] != null && dr[name].ToString() != "")
{
string value = dr[name].ToString();
SetModelValue(name, value, model);
}
}
list.Add(model);
}
dr.Dispose();
return list;
}
#endregion ************* 反射:将SqlDataReader反射成泛型集合 *************
#region ************* 反射:将DataTableReader反射成泛型集合 *************
/// <summary>
/// 将DataTableReader反射成泛型集合
/// </summary>
/// <typeparam name="T">实体对象</typeparam>
/// <param name="ds">DataTableReader</param>
/// <returns></returns>
public List<T> getModelArrary<T>(DataTableReader dtr) where T : new()
{
if (dtr == null || dtr.HasRows == false)
{
return null;
}
T t = new T();
List<T> list = new List<T>();
PropertyInfo[] properties = t.GetType().GetProperties();
while (dtr.Read())
{
T model = new T();
foreach (var propertyInfo in properties)
{
string name = propertyInfo.Name;
if (dtr[name] != null && dtr[name].ToString() != "")
{
string value = dtr[name].ToString();
SetModelValue(name, value, model);
}
}
list.Add(model);
}
dtr.Dispose();
return list;
}
#endregion ************* 反射:将DataTableReader反射成泛型集合 *************[/code]
全部源码在附件中
|
|