第一种:检测整个实体类
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Reflection;
using System.Data;
public partial class bangsotest_dr : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
IList<user> userlist;
string sqlcmd = "select top 1000 Username,Password from userinfo order by id";//这里的Username和Password会根据实体类中的对应字段,自动填充或跳过
using (SqlDataReader dr = Bangso.DBUtility.SQLHelper.ExecuteDataReader(sqlcmd))
{
userlist = FillEntityFromReader<user>(dr);
}
foreach (user u in userlist)
{
Response.Write(u.Username + "<br>");
}
}
public static IList<T> FillEntityFromReader<T>(SqlDataReader reader)
{
List<T> list = new List<T>();
List<PropertyInfo> pinfoList = new List<PropertyInfo>();
Boolean checkedPropertyInfo = false;
while (reader.Read())
{
T instance = Activator.CreateInstance<T>();
#region 检查数据库字段与实体类属性的匹配情况
if (!checkedPropertyInfo)
{
foreach (PropertyInfo pinfo in typeof(T).GetProperties())
{
//判断reader是否有此Property中的BoundFieldAttribute所对应需要绑定的字段
if (reader.GetSchemaTable().Select("ColumnName='" + pinfo.Name + "'").Length > 0)
{
pinfoList.Add(pinfo);
}
}
}
#endregion
//查检完成
checkedPropertyInfo = true;
//开始赋值
foreach (PropertyInfo info in pinfoList)
{
int index = reader.GetOrdinal(info.Name);
if (reader.GetValue(index) != DBNull.Value)
{
if (!info.PropertyType.IsEnum)
{
info.SetValue(instance, Convert.ChangeType(reader.GetValue(index), info.PropertyType), null);
}
else
{
info.SetValue(instance, Enum.Parse(info.PropertyType, reader.GetValue(index).ToString()), null);
}
}
}
list.Add(instance);
}
reader.Close();
return list;
}
public class user
{
string username;
public string Username
{
get { return username; }
set { username = value; }
}
}
}
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Reflection;
using System.Data;
public partial class bangsotest_dr : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
IList<user> userlist;
string sqlcmd = "select top 1000 Username,Password from userinfo order by id";//这里的Username和Password会根据实体类中的对应字段,自动填充或跳过
using (SqlDataReader dr = Bangso.DBUtility.SQLHelper.ExecuteDataReader(sqlcmd))
{
userlist = FillEntityFromReader<user>(dr);
}
foreach (user u in userlist)
{
Response.Write(u.Username + "<br>");
}
}
public static IList<T> FillEntityFromReader<T>(SqlDataReader reader)
{
List<T> list = new List<T>();
List<PropertyInfo> pinfoList = new List<PropertyInfo>();
Boolean checkedPropertyInfo = false;
while (reader.Read())
{
T instance = Activator.CreateInstance<T>();
#region 检查数据库字段与实体类属性的匹配情况
if (!checkedPropertyInfo)
{
foreach (PropertyInfo pinfo in typeof(T).GetProperties())
{
//判断reader是否有此Property中的BoundFieldAttribute所对应需要绑定的字段
if (reader.GetSchemaTable().Select("ColumnName='" + pinfo.Name + "'").Length > 0)
{
pinfoList.Add(pinfo);
}
}
}
#endregion
//查检完成
checkedPropertyInfo = true;
//开始赋值
foreach (PropertyInfo info in pinfoList)
{
int index = reader.GetOrdinal(info.Name);
if (reader.GetValue(index) != DBNull.Value)
{
if (!info.PropertyType.IsEnum)
{
info.SetValue(instance, Convert.ChangeType(reader.GetValue(index), info.PropertyType), null);
}
else
{
info.SetValue(instance, Enum.Parse(info.PropertyType, reader.GetValue(index).ToString()), null);
}
}
}
list.Add(instance);
}
reader.Close();
return list;
}
public class user
{
string username;
public string Username
{
get { return username; }
set { username = value; }
}
}
}
第二种根据实体类中的特性来选择填充哪些属性
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Reflection;
public partial class bangsotest_dr : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
IList<user> userlist;
string sqlcmd = "select top 100 username from userinfo";
using (SqlDataReader dr = Bangso.DBUtility.SQLHelper.ExecuteDataReader(sqlcmd))
{
userlist = FillEntityFromReader<user>(dr);
}
foreach (user ui in userlist)
{
Response.Write(ui.Username + "<br>");
}
}
public static IList<T> FillEntityFromReader<T>(SqlDataReader reader)
{
List<T> list = new List<T>();
List<PropertyInfo> pinfoList = new List<PropertyInfo>();
Boolean checkedPropertyInfo = false;
while (reader.Read())
{
T instance = Activator.CreateInstance<T>();
{
foreach (PropertyInfo pinfo in typeof(T).GetProperties())
{
foreach (BoundFieldAttribute attr in pinfo.GetCustomAttributes(typeof(BoundFieldAttribute), true))
{
//判断reader是否有此Property中的BoundFieldAttribute所对应需要绑定的字段
if (reader.GetSchemaTable().Select("ColumnName='" + attr.FieldName + "'").Length > 0)
{
pinfoList.Add(pinfo);
}
}
}
}
#endregion
//查检完成
checkedPropertyInfo = true;
//开始赋值
foreach (PropertyInfo info in pinfoList)
{
int index = reader.GetOrdinal(info.Name);
if (reader.GetValue(index) != DBNull.Value)
{
if (!info.PropertyType.IsEnum)
{
info.SetValue(instance, Convert.ChangeType(reader.GetValue(index), info.PropertyType), null);
}
else
{
info.SetValue(instance, Enum.Parse(info.PropertyType, reader.GetValue(index).ToString()), null);
}
}
}
list.Add(instance);
}
return list;
}
public class BoundFieldAttribute : Attribute
{
private string fieldName;
/// <summary>
/// 指定绑定的数据库字段名
/// </summary>
public string FieldName
{
get { return fieldName; }
}
/// <summary>
/// 初始化一个特性
/// </summary>
/// <param name="_fieldName">数据库字段名</param>
public BoundFieldAttribute(string _fieldName)
{
this.fieldName = _fieldName;
}
}
public class user
{
string username, password;
[BoundFieldAttribute("Username")]//这里的特性声明一定要,否则将无法绑定
public string Username
{
get { return username; }
set { username = value; }
}
public string Password//这里的Password将不会被填充,因为它没有声明特性
{
get { return password; }
set { password = value; }
}
}
}
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Reflection;
public partial class bangsotest_dr : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
IList<user> userlist;
string sqlcmd = "select top 100 username from userinfo";
using (SqlDataReader dr = Bangso.DBUtility.SQLHelper.ExecuteDataReader(sqlcmd))
{
userlist = FillEntityFromReader<user>(dr);
}
foreach (user ui in userlist)
{
Response.Write(ui.Username + "<br>");
}
}
public static IList<T> FillEntityFromReader<T>(SqlDataReader reader)
{
List<T> list = new List<T>();
List<PropertyInfo> pinfoList = new List<PropertyInfo>();
Boolean checkedPropertyInfo = false;
while (reader.Read())
{
T instance = Activator.CreateInstance<T>();
#region 检查数据库字段与实体类属性的匹配情况
if (!checkedPropertyInfo){
foreach (PropertyInfo pinfo in typeof(T).GetProperties())
{
foreach (BoundFieldAttribute attr in pinfo.GetCustomAttributes(typeof(BoundFieldAttribute), true))
{
//判断reader是否有此Property中的BoundFieldAttribute所对应需要绑定的字段
if (reader.GetSchemaTable().Select("ColumnName='" + attr.FieldName + "'").Length > 0)
{
pinfoList.Add(pinfo);
}
}
}
}
#endregion
//查检完成
checkedPropertyInfo = true;
//开始赋值
foreach (PropertyInfo info in pinfoList)
{
int index = reader.GetOrdinal(info.Name);
if (reader.GetValue(index) != DBNull.Value)
{
if (!info.PropertyType.IsEnum)
{
info.SetValue(instance, Convert.ChangeType(reader.GetValue(index), info.PropertyType), null);
}
else
{
info.SetValue(instance, Enum.Parse(info.PropertyType, reader.GetValue(index).ToString()), null);
}
}
}
list.Add(instance);
}
return list;
}
public class BoundFieldAttribute : Attribute
{
private string fieldName;
/// <summary>
/// 指定绑定的数据库字段名
/// </summary>
public string FieldName
{
get { return fieldName; }
}
/// <summary>
/// 初始化一个特性
/// </summary>
/// <param name="_fieldName">数据库字段名</param>
public BoundFieldAttribute(string _fieldName)
{
this.fieldName = _fieldName;
}
}
public class user
{
string username, password;
[BoundFieldAttribute("Username")]//这里的特性声明一定要,否则将无法绑定
public string Username
{
get { return username; }
set { username = value; }
}
public string Password//这里的Password将不会被填充,因为它没有声明特性
{
get { return password; }
set { password = value; }
}
}
}